Introduction
For many people, their major reason for learning a scripting language such as PHP is because of the interaction with databases it can offer.
In this tutorial, I will show you how to use PHP and the MySql database to store information on the web and include it into your website or application.
Before reading further, it is advisable that you should at least have a basic knowledge of how to use PHP as a language.
Why Would I Want A Database?
It is actually surprising how useful a database can be when used with a website. There are a huge variety of things you can do when you interact the two, from displaying simple lists to running a complete website from a database. Some examples of PHP and MySql being used together are:
- Banner Rotation. On this site, where each banner is, a PHP script is called. This opens a database and picks a random banner from it to show the visitor. It also counts the number of times the banner has been viewed and could, with a few changes, track clicks too. To add, change or edit the banners all I have to do is change the database and the script will pick the correct banners for all the pages on the site.
- Forums. Hundreds of forums (message boards) on the internet are ran using PHP and MySql database. These are much more efficent than other systems that create a page for each message and offer a wide variety of options. All the pages in the forum can be updated by changing one script.
- Databases. One quite obvious example are websites that get all their information from a database. For example Script Avenue is ran by few scripts that gain all their information from a large database. All the different script categories can be accessed in one script by just changing the URL to access a different part of the database.
- Websites. If you have a large website and you want to change the design it can take a very long time to update and upload all the pages. With PHP and MySql, your whole website could be just one or two PHP scripts. These would access a MySql database to get the information for the pages. To update the website's design, you would just have to change one page.
What Do I Need?
You only really need three things to run PHP scripts which access MySql databases. Firstly, you will, of course, need a webserver. This can either be on a computer of your own or on a web host. Any web server software should work with PHP and MySql but the best to use is Apache, which is free.
PHP also needs to be installed on the server. If it is not already installed you can install it (or ask your web host to install it). It can be downloaded from PHP.net and is also free. If you are not sure if you have PHP installed I will show you a way to check it later.
Finally, you will also require MySql. This is the actual database software. You can also use most other types of database (SQL, Oracle etc.) but as this is a PHP and MySql tutorial, I will deal just now with the MySql database although the commands used here will also work with SQL databases. As with the other software you need, MySql is free and can be downloaded from the MySql homepage. If you are not sure if you have MySql installed, I will show you how to check later.
If you cannot install (or your web host won't allow) PHP and MySql you can still use another web host. Freedom2Surf are a free (banner supported) web host and support PHP and have MySql installed. NameDotCom is as well a good server that supports all you need. HostRocket are an excellent web host and can offer you 300MB of space with PHP, MySql and loads of other extras for under $10 a month.
Testing For PHP and MySql
There is a simple test for both PHP and MySql . Open a text editor and type in the following:
<?php
phpinfo();
?>
and save it as phpinfo.php
Now upload this to your webspace and go to it in your browser. If you have PHP installed you will see a huge page with all the details of your PHP installation on it. Next, scroll down through all this information. If you find a section about MySql then you will know that MySql is installed.
Managing Databases
Although all the database administrative options can be done through PHP scripts, I strongly suggest installing a copy of
XAMPP which is a server on its own that helps you to run your websites or applications on a local host which is your computer before uploading your files to the remote server. This software is an excellent free set of scripts that will provide you with an administrative interface for your MySql database(s). You can add, remove, edit, backup and view your databases using this and it is especially useful when troubleshooting your databases.
This Tutorial
Throughout this tutorial I will be showing you some of the basics of using PHP and MySql together. To do this I will be using an example all the way through. As you use this tutorial, you will learn how to create a web based contact management program. It will allow you to store names with their addresses, e-mail and phone numbers. You will be able to update records and search the database. There will even be an option which allows you to send an e-mail out to all the people in the database (please note: this system should not be used for spam or unsolicited e-mail).
After creating this system you should have enough knowledge to go on and create nearly any type of database enabled website you want to.
The next tutorial in this series will be on
Setting Up a Database.