PHPBuddy.com
 PHP Function Lookup:
 
Categories
PHP Quick Start
PHP Installation
PHP Articles
PHP Scripts

Top Rated Articles 
  • Simple Ad Rotator
  • PHP and Cookies
  • Getting Screen resolution using JavaScripts & PHP
  • Using Functions in PHP
  • PHP Sessions

  • Site Related
    Submit Articles/Code
    Contact Us
    Home

       Home                   Article Added on: March 4, 2002
    Working with Database

    Overview: In this section you will learn how to make PHP work with databases.

    Okay I admit this is what most of you were waiting for creating a database driven site using PHP, Initially we will be working with MySQL database but dont worry its very easy to connect to other databases I will show you how to connect to other databases later on.

    Syntax for connecting to a MySQL database
    PHP has built in support for connecting to MySQL. The following PHP function call establishes the connection:

    mysql_connect(<address>, <username>, <password>);

    address: It is the IP address or hostname of the computer on which the MySQL server software is running, if MySQL is running on the same machine on which PHP is installed we use "localhost"

    username: As the name suggests is the MySQL user account name.
    password: Contains the password of the MySQL database.

    The mysql_connect function shown above, returns a number that identifies the connection that has been established. Since we intend to make use of the connection, we should hold this value in a variable, lets assume that our MySQL database is on a localmachine and the username is root and password is pass and we will be storing the connection in a variable $db. Then the mysql_connect function will look something like this

    $db = mysql_connect("locahost","root","pass");

    Now that we are connected to MySQL database the next step is to select the database we wish to use to select the database we use the function mysql_select_db the syntax of which is given below

    mysql_select_db(Database name,database connection string);

    Let assume that we will be connecting to mybuddy database, so you code will be

    mysql_select_db("mybuddy",$db);

    That is it we have connected to MySQL database, see it so simple its just 2 lines of code

    $db = mysql_connect("localhost","root","pass");
    mysql_select_db("mybuddy",$db);


    Before we can start retriving data from our database we need some tables with some data in our database so that we can work with them!


    Next: Displaying data from a database table

     
    Rate this article:  Current Rating: 3.67
      Poor    Excellent     
              1     2    3    4    5

     

    Other Related articles:

    Home | Privacy Policy | Contact Us | Terms of Service
    (c) 2002 - 2017 PHPbuddy.com Unauthorized reproduction/replication of any part of this site is prohibited.