Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Steps to get data from MySQL via PHP by DRJ478
Posted: 7 Jul 03 (Edited 3 Jun 05)

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


$hostLink = mysql_connect($host,$user,$pass) OR die('Unable to connect: '.mysql_error());
or for persistent connections use


$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


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

# Step 3: Execute a query


$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


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:


$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 error checking 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.

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close