Jump to content

[SOLVED] loop through each row for a result


thewooleymammoth

Recommended Posts

Ive been searching for a way to do this but there is so many results for these key words i cant find anything so sorry if this has already been answered.

 

what im trying to do is essentually setup a mail inbox. i have a database setup with a reciver field a subject field and a date field(and some other irrelevant fields). and what i need to do is query for all rows with the recipient being the user loop through each row returned to display each message for that user. I cant seem to figure out how to do that. mysql_fetch array and mysql_fetch_assoc only return one row.

 

here is my mysql query

 

SELECT *
FROM `dev_messages` 
WHERE `reciever` = '$uname'

$sql = "SELECT * FROM `dev_messages` WHERE `reciever` = '$uname'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
 print_r($row);
}

 

edit: replace the print_r() part with whatever print statements you want for each entry

$sql = "SELECT * FROM `dev_messages` WHERE `reciever` = '$uname'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
  print_r($row);
}

 

edit: replace the print_r() part with whatever print statements you want for each entry

 

so each time you fetch array it gives you the next result?

yes

 

http://us3.php.net/mysql_fetch_array

Description

array mysql_fetch_array ( resource $result [, int $result_type= MYSQL_BOTH ] )

 

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.