dalehay.com
Search Engine Optimization

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 2
Change
YOUR_HOST
to your MySQL host name. Change
YOUR_USERNAME
to your MySQL username. Finally, change
YOUR_PASSWORD
to your MySQL password.

Example
<?php
mysql_connect("localhost","dale","password");
?>
Step 3
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).
phpMyAdmin
mysql_select_db("YOUR_DATABASE");
Replace
YOUR_DATABASE 
with 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.

Comments

Comments:

Add your comment...

Your Name:

Your Email:

Your Comment:

Enter This Number:

(53820)


Note: Your IP address is saved into the database when you submit a comment. Any type of threatening behaviour will result in your ISP being contacted and legal action being taken place!





Latest News Stories
» Not good...
» Internet Explorer 8 Problems
» October
» Relaxtion by Dale Hay
» BBC Weather Revamp
» Dog Singing
Top Viewed Stories
» PHP Mini Message Board Tutorial (4,211)
» Rare McDonalds Monopoly 2008 Tickets (3,051)
» PHP Search Engine Tutorial (2,271)
» Helen Willetts Pregnant? (1,399)
» QI Series 6 on BBC One (900)
» Create your own Image Uploader (873)
Most Commented Stories
» PHP Mini Message Board Tutorial (33)
» Create your own Image Uploader (18)
» I like my DuMP (15)
» I'm on Wikipedia! (7)
» Chocie Munchin Niece (6)
» Life on Mars... (6)