PHP MySQL Tutorial

01
of 05

Connect to MySQL

Interacting with MySQL makes PHP a far more powerful tool. In this tutorial, we will go through some of the most common ways PHP interacts with MySQL. To follow along with what we are doing, you will need to create a database table by executing this command:

 CREATE TABLE friends (name VARCHAR(30), fav_color VARCHAR(30), fav_food VARCHAR(30), pet VARCHAR(30)); 
 INSERT INTO friends VALUES ( "Rose", "Pink", "Tacos", "Cat" ), ( "Bradley", "Blue", "Potatoes", "Frog" ), ( "Marie", "Black", "Popcorn", "Dog" ), ( "Ann", "Orange", "Soup", "Cat" ) 

This will create a table for us to work with, that has friends' names, favorite colors, favorite foods, and pets.

The first thing we need to do in our PHP file is connected to the database. We do that using this code:

 

Of course you will replace server, username, password, and Database_Name with the information relevant to your site. If you are unsure what these values are, contact your hosting provider.

02
of 05

Retrieve Data

Next we will retrieve the information from the database table we created called "friends"

 // Collects data from "friends" table 
 $data = mysql_query("SELECT * FROM friends") 
 or die(mysql_error()); 

And we will then temporally put this information into an array to use:

 // puts the "friends" info into the $info array 
 $info = mysql_fetch_array( $data ); 

Now let's print out the data to see if it worked:

 // Print out the contents of the entry 
 Print "Name: ".$info['name'] . " "; 
 Print "Pet: ".$info['pet'] . " 
";

However this will only give us the first entry in our database. In order to retrieve all the information, we need to make this a loop. Here is an example:

 while($info = mysql_fetch_array( $data )) 
 { 
 Print "Name: ".$info['name'] . " "; 
 Print "Pet: ".$info['pet'] . " 
"; }

So let's put all the these ideas together to create a nicely formatted table with this final php code:

 "; 
 while($info = mysql_fetch_array( $data )) 
 { 
 Print ""; 
 Print "Name: ".$info['name'] . " "; 
 Print "Pet: ".$info['pet'] . " "; 
 } 
 Print ""; 
 ?> 
03
of 05

SQL Queries with PHP

Now that you have done one query, you can do more complicated queries using the same basic syntax. If you have forgotten the queries, you can review them in the MySQL glossary.

Let's try to do a query of our database for people who have cats for a pet. We will do this by adding a WHERE clause to set pet equal to Cat.

 "; 
 while($info = mysql_fetch_array( $data )) 
 { 
 Print ""; 
 Print "Name: ".$info['name'] . " "; 
 Print "Color: ".$info['fav_color'] . " "; 
 Print "Food: ".$info['fav_food'] . " "; 
 Print "Pet: ".$info['pet'] . " "; 
 } 
 Print ""; 
 ?> 
04
of 05

Create Tables

Following this same structure, we can connect to a database and create new tables. At the end we will print a line, so we know that it is done executing:

 

Print "Your table has been created"; ?>

I find this method is often used when installing a PHP program someone else has written. Often an install file includes a way for the user to update the MySQL database from the browser. This allows people less familiar with the code to install the program more easily.

05
of 05

Insert Into Tables

We can use the same method of using SQL commands to populate our database as we did to create it. Here is an example:

 

Print "Your table has been populated"; ?>

Format
mla apa chicago
Your Citation
Bradley, Angela. "PHP MySQL Tutorial." ThoughtCo, May. 22, 2017, thoughtco.com/php-mysql-tutorial-2694114. Bradley, Angela. (2017, May 22). PHP MySQL Tutorial. Retrieved from https://www.thoughtco.com/php-mysql-tutorial-2694114 Bradley, Angela. "PHP MySQL Tutorial." ThoughtCo. https://www.thoughtco.com/php-mysql-tutorial-2694114 (accessed January 18, 2018).