Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Steps to get data from MySQL via PHP

PHP & MySQL

Steps to get data from MySQL via PHP

by  DRJ478  Posted    (Edited  )
There is often confusion among beginners on how to retrieve data from a MySQL server. This FAQ explains the basic steps.

#Step 1: Connect to the MySQL host
Code:
$hostLink = mysql_connect($host,$user,$pass) OR die('Unable to connect: '.mysql_error());
or for persistent connections use
Code:
$hostLink = mysql_pconnect($host,$user,$pass) OR die('Unable to connect: '.mysql_error());
The variable $hostLink is a resource identifier that is used to direct further commands to the specified host. You could have connections to more than one host. Please note the error handling in the OR die() clause. Show the errors so you notice them. Hiding errors with the @ operator will ultimately make debugging very hard.

#Step 2: Select a database
Code:
mysql_select_db($dbName, $hostLink) OR die('Unable to select database: '.mysql_error());
This function only returns true/false.

# Step 3: Execute a query
Code:
$result = mysql_query($SQL,$hostLink) OR die ('Query error: '.mysql_error());
Here is where most people get stuck.
mysql_query() returns different things for different queries. The only commonality is that it returns FALSE if there is an error executing the SQL statement. The OR die() catches those errors.
mysql_query() does not return any data from the queried tables.
It may return TRUE to indicate that the SQL command was processed successfully.
For commands that retrieve data from or about a table $result is a resouce identifier that has to be used to retrieve the actual data from the query in the next step.

#Step 4: Get the data or information about the result set
You have different commands at your disposal.
To read record by record use
Code:
while ($row = mysql_fetch_array($result)){
   # your processing code
}
Read the PHP documentation for mysql_fetch_array() to learn about the different format options: http://www.php.net/manual/en/function.mysql-fetch-array.php

If you want to know how many rows in the set:
Code:
$num = mysql_num_rows($result);

There are many other mysql_commands to manipulate a result set: http://www.php.net/manual/en/ref.mysql.php
Also note that the newer optimized MySQL commands can be found under the mysqli family: http://www.php.net/manual/en/ref.mysqli.php

#Most important:
Add [color red]error checking[/color] to all lines that allow for it. Often failed commands show their effects much later in the code when a dependant chunk of code is executed. The error message will make you search in the wrong place. Whenever you have a chance to check if your command was successful, do it.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top