PHP tutorial - How to connect and fetch data from mySQL database in PHP

best php web server

How To Connect And Fetch Data From MySQL Database In PHP

mySQL is one of the most widely used opensource application in the world alongside with PHP and Apache. If combined together, it could simply form the best utilities to develop dynamic web sites.

Before you can gather data from your database, you have to make a connection to it. The command to mySQL connection is mysql_connect( )

<?php

$conn = mysql_connect("localhost", $userName, $passWord);
mysql_select_db($DB);

?>

The mysql_connect( ) takes three arguments. First is the hostname, usually it is 'localhost' or your IP address that run mysql server. Second is username, this is the username to mysql username. And the last one is password! I'm sure you know what it is.

After telling PHP to connect, now you should tell PHP what database to select. This can be done by using mysql_select_db( ) function and place the name of your database between those two parentheses.

There are several method to fetch data from mySQL DB. But I will only explain this mysql_fetch_array( ) !

For example you have this in your DB

ID Name Age
1 - David - 25
皜 - John - 20

What we want to do is to print out a result like this using mysql_fetch_array( ):

Name = David
Age = 25

----------------

Name = John
Age = 20

----------------

First, we have to make query to mySQL database by using mysql_query( ) function. This tells PHP what to look for in DB.

<?php

$conn = mysql_connect("localhost", $userName, $passWord);
mysql_select_db($DB);
$sql = mysql_query("SELECT * FROM tableName");

?>

When creating SQL queries, it's always a good idea to ask yourself this question: "What do I want, and where does it from?" If I want to select ID, then I have to write 'SELECT ID FROM tableName'. If I want to select both ID and Name, then I have to write 'SELECT ID, Name FROM tableName'

In the case above, 'SELECT * FROM tableName' will tell mySQL to select all fields from tableName. The ' * ' represents all! Now, it's time to print out the result

<?php

$conn = mysql_connect("localhost", $userName, $passWord);
mysql_select_db($DB);
$sql = mysql_query("SELECT * FROM tableName");
while($result = mysql_fetch_array($sql)) {
echo " <b>Name</b> = " .$result["Name"] . " <br>";
echo " <b>Age</b> = " .$result["Age"] . " <br><br>----------------<br><br>";
}

?>

In previous tutorial, I had explain that the while will continue to loop until the condition is false. In the code above, the while( ) will loop until it reach the end of the data entries. When this code is executed, it will shows

Name = David
Age = 25

----------------

Name = John
Age = 20

----------------

  

Optimize Website - Webmasters optimize their site and gain higher search engine ranking

Free Photo Hosting - Free File Hosting, Free ZIP file Hosting, Free Image Hosting, Free MP3 Hosting.

Funny Videos, Stupid Videos

Stupid Videos - Custom Pimped Motorcycle Pictures, Videos, and Articles

Forectory - Webmaster Directory

St Louis Computer Networking

Website Templates - 1000s of Templates $5 - Many Templates to help you build your site!

 


Each and every tutorial published in PHPhelps.com is originally written by PHPHELPS OWNERS. None of the content is allowed to be replicated without prior notice to writter.
Copyright © 2006 PHPhelps.com