An introduction to PHP and MySQL

John John (304)

In this guide I'm going to describe how to get started with PHP and MySQL. Many books have been written on this subject, but I'm just going to cover the basics of getting connected to a MySQL database and executing simple queries. This assumes that you've set up a MySQL database and have the proper privileges necessary to run the queries you prefer. This also assumes that you have a basic understanding of how to create and run a PHP script.

This guide should be applicable to most up-to-date PHP editions but you can visit this guide to learn how to check your PHP version. If you want to modify permissions for your project, check out our guide on how to create users and grant privileges in MySQL.

Posted in these interests:
h/php8 guides
h/mysql9 guides
$link = mysql_connect( 'localhost', 'username', 'password' );
if ( ! $link ) die( mysql_error() );

The first line simply opens the PHP script and allows the interpreter to know that PHP is contained within.

The next line actually makes the connection to the database. The first parameter is a string containing the hostname, the second parameter is a string containing the username, then the third parameter is a string containing the password. A successful connection will return a resource containing the mysql connection. Any connection problem will simply return false.

The third part checks for a false link and prints the mysql error if there is one.

$db = mysql_select_db( 'database_name' );
if ( ! $db ) die( mysql_error() );

The first line selects a specified database and returns false if there is a problem. The first parameter to 'mysql_select_db' is a string containing the database name.

The second part, like the previous step, simply checks for a false response signifying an error.

$res = mysql_query( "select * from table_name" );
if ( ! $res ) die( mysql_error() );

This line actually runs a mysql query. To start I recommend running a select query like the one provided in the example. To run a query you will need to know the name of the table. This function returns a mysql resource containing the query results.

As before, if this function returns false, there was an error so we will stop the script.

if ( mysql_num_rows( $res ) > 0 ) {
   while ( $row = mysql_fetch_array( $res ) ) {
      print_r( $res );

The first line is a condition testing the number of rows return by the query. If there is more than one row we will continue.

The second part contained within the conditional is a while loop that will loop through each of the results of the query result. 'mysql_fetch_array' is a function that takes each row of the mysql resource response and converts it into a PHP array.

Finally, the third part contained within the loop will simply print the PHP array so we can see the contents. Check out this guide for detailed steps on printing an array in PHP.

If you found this guide useful, you may want to check out our guide on checking PHP syntax via command line or some of our more specific MySQL tutorials like this one on how to rename a MySQL table.

John John (304)
1 minute

This guide will show you how to install the PHP mycrypt module on RHEL 6.