PHP Tutorial - Databases in PHP - MySQL in PHP - hooking up

By Vince Barnes

Since we are about to get into the use of a specific DBMS in PHP, namely MySQL, I should warn you of one thing to watch out for regarding this DBMS.  In the past, the support for MySQL, including all the functions used to interact with your MySQL database, was built in to PHP.  When they came out with PHP 5, however, this support was no longer included.  Obviously, if the support isn't there, it won't work -- in fact you'll get error messages saying "undefined function mysql_......"   If you get such a message it means that the support for MySQL will have to be added to your PHP.  How to do that is beyond the scope of this tutorial, but you'll  find help at the MySQL website http://www.mysql.com and/or at the PHP website http://www.php.net where you'll find the PHP Manual, which has all kinds of good things you'll want to know.

Connect to MySQL Server

The first thing you'll want to do is to establish a connection to your MySQL database server.  This is accomplished with the mysql_connect function. Here's an example:

$connid = mysql_connect ('servername' , 'username' , 'password');

$connid is an identifier (a positive integer, returned by the mysql_connect function) that identifies this connection and is used in subsequent function calls to tie those calls to this connection.  The mysql_connect function is provided three parameters, the name of the server (commonly 'localhost' when the server is on the same computer as PHP), the username and password authorized to access the server.

Next, we need to check that the connection was successfully established.  For the sake of this example we are going to print a message on the screen to let us know of whether or not the call was good.  In practice, you will probably take a different course of action (see also the "error control operator, below.)

if ($connid = = false)
 { print "Server connection failed";  }
 { print "Server connected!"; }

When we are all done, it is a sound programming practice to close things that we have opened.  To close our connection we will use the mysql_close function.  Notice that we use the $connid to reference the connection that we opened.

mysql_close ($connid);

Nice and tidy!

Using the Error Control Operator

The error control operator can be used to prevent PHP from displaying an error message in the user's browser when the attemp to connect to the MySQL server fails.  In the real world, this is the more common requirement.  The error control operator is an @ sign placed next to the function call like this:

$connid = @mysql_connect ('servername' , 'username' , 'password');

Using the die function

Another common real world practice is to use the die function to terminate the program and display a message.  Without the connection to the database, it's most likely that there's no point in continuing!

$connid = @mysql_connect ('servername' , 'username' , 'password') or die("Connection to the DBMS server failed");


Now that we have connected to the server, we'll need to select a database and start working with it.  That's next!  (Coming soon)

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date