As of PHP 5.5 the mysql extension has been officially deprecated. In a joint community effort we will update tutorials using the recommended PDO or MySQLi extensions.

As in many languages, there are 150 different ways to do just about everything, the same rule applies for a simple connection and output of data in a mySQL database using PHP. This tutorial will give you only one way of accomplishing this, and while it works, it is definitely not the only way.

We’ll assume you already have some sort of database setup, whether it’s from a pre-installed script, or a database of your own creation, it doesn’t matter. It should at least have a few records in it for testing purposes.

For the sake of this tutorial, we will place all of the code in one file, however, it is good practice to place the connection variables and connection string in a separate config type file which can be included each time you make the database connection.

So lets get started, first we should define out server, username, password, and the database we will be connecting to. The easiest way to do this is with a few variables:

Alrighty, so now we’ve got an open connection to the database, from this point we can pass SQL commands. For this tutorial we’ll just be using a SELECT statement.

If you were using an INSERT, UPDATE, or DELETE statement you would use another function: exec(). The reason for this change is that INSERT, UPDATE, and DELETE statements do not return a result set. The exec() function, however, hands us the number of rows affected by its statement so we can verify the success of our action.

This will result in all records from the 'members' table being retrieved and stored in the $sql_result variable. Since we’ve selected the id, username, and email columns, we will need to pull them back out in to a usable form.

To fetch the results of our previos SELECT query, we put the $sql_result variable into a foreach loop. At the beginning of the loop it will pull each row from the $sql_result variable. Each time the foreach loop runs, it goes to the next row1.

So that’s it, you’ve connected to the database, executed a SQL statement, retrieved the records, and displayed them using the actual column name as the variable.

Editor’s notes:

/1/ - At this point the original tutorial used extract() to convert each field into its own variable. Although this in short terms saves you some key strokes, it later bites nastily back, because it creates untraceable variables that can overwrite variables set elsewhere in your code (e.g. the $username variable that stores the currently logged-in user). As a rule-of-thumb only ever create variables through $variable = 'value';

Thank you sooo much for this tutorial! I've been searching and trying things for weeks and I finally have some results to play with that I understand. I'm a total newbie to mySQL and PHP so no doubt i'll be back here for lots of help but just wanted you to know i feel like i've found a 'home' lol. Thanks again