Connecting to MySQL with PHP
PHP/MySQL
Last Updated: 2007-01-27 13:18:24
Last Updated: 2007-01-27 13:18:24
STEP 1: Using mysql_connect()
Before you start to use MySQL with PHP you must make sure that you have MySQL running and have created a valid account with a username and password. You must also know the name of the database you are trying to access.
Using the mysql_connect() function is the first step to using MySQL with PHP. The syntax for using this function is as follows:
<?
mysql_connect("hostname", "username", "password");
?>
"Hostname" represents the path to your running version of MySQL. If you are using a local running version (Example: with a LAMP or MAMP install) your hostname may be along the lines of "localhost:8889" or just "localhost". If you are using a networked connection your hostname may be an IP address or a URL like mysql.yourhost.com.
I like to put my username and password as variables outside of the function. The reason I do this is because one time my database server crashed and an error report was broadcast throughout my site stating: Could not connect: localhost, joesusername, joespassword. So now everyone that came to my site knew the username and password to my database.
The syntax for this looks like so:
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password");
?>
Using the mysql_connect() function is the first step to using MySQL with PHP. The syntax for using this function is as follows:
<?
mysql_connect("hostname", "username", "password");
?>
"Hostname" represents the path to your running version of MySQL. If you are using a local running version (Example: with a LAMP or MAMP install) your hostname may be along the lines of "localhost:8889" or just "localhost". If you are using a networked connection your hostname may be an IP address or a URL like mysql.yourhost.com.
I like to put my username and password as variables outside of the function. The reason I do this is because one time my database server crashed and an error report was broadcast throughout my site stating: Could not connect: localhost, joesusername, joespassword. So now everyone that came to my site knew the username and password to my database.
The syntax for this looks like so:
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password");
?>
STEP 2: Using mysql_select_db()
Establishing a connection is half the battle. Now we need to tell MySQL what database we wish to connect to. To do this we use the mysql_select_db() function. The syntax for this is as follows:
mysql_select_db("yourDB", "yourConnection");
Adding this to our script we have now established a connection to the MySQL database and told MySQL which database to connect to.
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password");
mysql_select_db("yourDB", $conn);
?>
mysql_select_db("yourDB", "yourConnection");
Adding this to our script we have now established a connection to the MySQL database and told MySQL which database to connect to.
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password");
mysql_select_db("yourDB", $conn);
?>
STEP 3: Error Messages and Closing the Connection
Now if there is a problem and we are not getting a connection, we need an error message to find out why. To get an error message we use the mysql_error() function. To implement this we append an "or DIE" statement to our connection script. The "or DIE" statement will quit PHP when an error is thrown by our connection script and give us a chance to post an error message using mysql_error().
The syntax is as follows:
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password") or DIE("There was an error: " . mysql_error());
mysql_select_db("yourDB", $conn);
?>
This should provide us with a nice detailed error if our script runs into trouble.
Finally we want to close our connection. The connection will close by itself when the user leaves the page, but a cleaner way to work is to explicitly close the connection yourself with the mysql_close() function.
We place this at the end of our script when we are done using MySQL.
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password") or DIE("There was an error: " . mysql_error());
mysql_select_db("yourDB", $conn);
// our queries would go between the connection and the close statements.
// an example query is as follows
$sql = "SELECT name FROM products WHERE id = '4'";
$result = mysql_query($sql) or DIE("SQL ERROR: " . mysql_error());
mysql_close($conn);
?>
The syntax is as follows:
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password") or DIE("There was an error: " . mysql_error());
mysql_select_db("yourDB", $conn);
?>
This should provide us with a nice detailed error if our script runs into trouble.
Finally we want to close our connection. The connection will close by itself when the user leaves the page, but a cleaner way to work is to explicitly close the connection yourself with the mysql_close() function.
We place this at the end of our script when we are done using MySQL.
<?
$username = "myUsername";
$password = "myPassword";
$conn = mysql_connect("hostname", "$username", "$password") or DIE("There was an error: " . mysql_error());
mysql_select_db("yourDB", $conn);
// our queries would go between the connection and the close statements.
// an example query is as follows
$sql = "SELECT name FROM products WHERE id = '4'";
$result = mysql_query($sql) or DIE("SQL ERROR: " . mysql_error());
mysql_close($conn);
?>
This is the JoeLucky39 tutorial section. Feel free to use whatever information you find here on your own site(s). Know that not all tutorials are ideal for all sites, so feel free to modify the information contained in this tutorail section as best suits your web site.
At JoeLucky39 you can find tutorials on Comic Art, PHP/MySQL, and more are added all the time. So, visit often and feel free to contact Joe if you think that his tutorials need to be changed or updated.
Joe believes in the open source movement and is happy to share his knowledge with anyone who asks. All Joe asks is, if you use Joe's open source tutorials, you share your knowledge as well.
Thank you and I hope you find what you are looking for. If not feel free to contact me and ask.
