Jump to content


Photo

MySQL Data Retrieval


  • Please log in to reply
No replies to this topic

#1 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 09 June 2006 - 03:12 PM

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:

http://www.php.net/mysql_fetch_array
http://www.php.net/mysql_fetch_assoc

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

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users