« « Tutorial: How To Create A Database With PHP & MySQL – Part 2 | Quick Shapeoko DW660 Spindle Mount » »

Tutorial: How To Create A Database With PHP & MySQL – Part 3

Feb 23, 2014 by     No Comments    Posted under: PHP, Tutorials

This is the third and final part of my tutorial series on creating a database with PHP and MySQL. You can find the first and second parts here and here.

So far we’ve designed our product database, created a new database in Siteground, and populated the database with some items from our imaginary bike shop. Now we need to display our products on a webpage so our customers can see what we offer.

Create a new PHP file named store.php, and save it somewhere on your server. There are two steps to creating a product list. First, we need to tell PHP how to connect to our database. Then, we can connect and get a list of products.

Let’s connect to the database. Add the following lines to the top of your PHP page.

<?php

$username = "johndoe";
$password = "password";
$hostname = "localhost";

$db = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to MySQL");

Obviously you’ll want to change $username and $password to the user and password you created in part one of this tutorial. If you are hosting your files on Siteground, you can leave $hostname as localhost. Otherwise, set it to the IP address or domain address of your database.

What does “or die” mean? That’s a simple error handler that says, if we can’t connect to the database for whatever reason, don’t bother running the rest of the PHP code, and show us any error message explaining what happened.

Now let’s tell PHP which database we’ll be using. We named the database “example_bike-store-example”, so tell PHP to look there. If you named your database something different, use that name instead.


$selected = mysql_select_db("example_bike-store-example",$db)
    or die("Could not select examples");

Now comes the fun part. Let’s display our products!


$result = mysql_query("SELECT * FROM products");

The asterisk is a wildcard, so this query says select all the fields you find from the products table. This will return a resource containing the list of products that matched the query. All we need to do now is display them.


while ($row = mysql_fetch_array($result)) {
    echo "Product Id: " . $row{'id'} . "Name: " . $row{'product_name'} . "Description:  " . $row{'product_description'} . "Price:  $" . $row{'product_price'} . "<br>";
}

This will print out a nice list of all our products. If you wanted to create a table to display the products, this is the place to do it. The last step is to close the database connection. PHP will automatically close the connection when the script ends, but it’s good practice to always clean up after yourself.


mysql_close($db);

Congratulations, you just created your very own database, and connected it to a web page! Now you’re ready to use what you learned on your own website. Good luck!

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>