Connect to Database in PHP / MySQL
Digg! This Page
If you have noticed on many websites like profile based ones or song lyric websites they have thousands upon thousands of pages. Now obviously someone couldn't have had sat there and created every single viewable page one by one. Most, if not all of the pages content comes from a database. If you use PHP then using a MySQL database would be your best option as it's free and easy to use, plus most web hosts that offer PHP also offer MySQL. Below is a easy tutorial on how to connect to a MySQL database in PHP - you will need to have this at the top of any database type coding on any PHP page.Step 1
We need to establish a connection to our MySQL database, so if you know your connection settings you can use this code (within the PHP tags)
mysql_connect("YOUR_HOST","YOUR_USERNAME","YOUR_PASSWORD");
Step 2Change
YOUR_HOSTto your MySQL host name. Change
YOUR_USERNAMEto your MySQL username. Finally, change
YOUR_PASSWORDto your MySQL password.
Example
<?php
mysql_connect("localhost","dale","password");
?>
Now you have made a connection, you will need to now tell the database what database you actually want to read. To find out, if using phpMyAdmin then it'll be above the tables within your database. (Check the shaded area in the screenshot below).

mysql_select_db("YOUR_DATABASE");Replace YOUR_DATABASEwith the name of your database, which is shown in the faded red colour on the screenshot as "dalehay". If you don't know what it is then ask your web hosting service / company.
Example
<?php
mysql_connect("localhost","dale","password");
mysql_select_db("dalehay");
?>All this will make the database connection and also connect to the correct database.


