Using MySQL with PHP – Part 1

This is a very simple tutorial intended to explain how to connect to a MySQL database with PHP and how to retrieve and manipulate information from that database.

Prior Steps

Before you actually connect to a database, you need to have a database to which to connect. Further, you need to have a table inside of that database that contains information. I will write up a simple tutorial explaining how to create new databases and add information to them in another tutorial, soon.

You will also need to know the username and password for a MySQL user that has, at least, SELECT permissions on the database and you will need to know the name of the “local” MySQL host (generally “localhost,” but it will sometimes deviate to things like “MYSQLHOST” or something like that). If worse comes to worse, you can try using the IP address of your Web server, if you know it.

Additionally, you need to have PHP installed and set up with the MySQL module set up (which, if you are not setting up your own Web server, is not generally something you ever have to worry about).