Jump to content

MySQL Data Retrieval


Recommended Posts

NOTE: This information is now obsolete, as the mysql_ functions are deprecated and have been removed from PHP as of php5.5.

 

Use the mysqli or PDO_Mysql extensions instead.  

 

 

 

 

 

MySQL Data Retrieval

Q:  I have inserted the data into a MySQL database, but how do I show it?  (I sometimes get "Resource id#")
A:  To do this, you should use mysql_fetch_array, or in many cases, mysql_fetch_assoc.  The first one obtains an array where you can either use the column name, or a row number.  Since I don't think in numbers, I use the second one, which only obtains an array of the column names.  More info:

[url=http://www.php.net/mysql_fetch_array]http://www.php.net/mysql_fetch_array[/url]
[url=http://www.php.net/mysql_fetch_assoc]http://www.php.net/mysql_fetch_assoc[/url]

To do this, let's assume you have a query set up.  In this case, an example would be:

$sql = "SELECT name, email FROM people_table";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
    echo $row['name'].'<br />';
    echo $row['email'].'<br />';
}
The while() loop will go through all the data in the table, and when it reaches the end (when there's no more data to go through), it will stop.  Notice how the declaration of the $row variable is not used with the equality operator, ==.


Acknowledgement: steveo31
Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.