Jump to content

mysql_query that returns multiple rows?


MoMoMajor

Recommended Posts

I have a mysql query that should be returning multiple rows but it only returns information from the first row

 

this is my query

 

$result3=mysql_query("SELECT * FROM images WHERE p_id ='$id_of_page'");

 

$image_returns=mysql_fetch_array($result3);

$id_of_image = $image_returns['id'];

$name_of_image = $image_returns['image_name'];

$file_name_of_image = $image_returns['file_name'];

$p_id_of_image = $image_returns['p_id'];

$date_of_image = $image_returns['date'];

$file_type_of_image = $image_returns['file_type'];

$file_size_of_image = $image_returns['file_size'];

 

how do i get the data from all of the rows stored in these variables?

 

 

Link to comment
https://forums.phpfreaks.com/topic/167186-mysql_query-that-returns-multiple-rows/
Share on other sites

while($image_returns = mysql_fetch_assoc($result3)) {
$id_of_image = $image_returns['id'];
$name_of_image = $image_returns['image_name'];
$file_name_of_image = $image_returns['file_name'];
$p_id_of_image = $image_returns['p_id'];
$date_of_image = $image_returns['date'];
$file_type_of_image = $image_returns['file_type'];
$file_size_of_image = $image_returns['file_size'];

//Print data

}

while($image_returns = mysql_fetch_assoc($result3)) {
$id_of_image = $image_returns['id'];
$name_of_image = $image_returns['image_name'];
$file_name_of_image = $image_returns['file_name'];
$p_id_of_image = $image_returns['p_id'];
$date_of_image = $image_returns['date'];
$file_type_of_image = $image_returns['file_type'];
$file_size_of_image = $image_returns['file_size'];

//Print data

}

 

Thanks for the very fast response Matthew. I put this code into my script and it returned the last image instead of displaying all of them. Overall I am trying to make this script a simple image viewer but the script only ever wants to return information on one image at a time. I don't understand how I can make the distinctions between the different rows in mysql.

Did you output the data within the while?

 

Something like

while($image_returns = mysql_fetch_assoc($result3)) {
$id_of_image = $image_returns['id'];
$name_of_image = $image_returns['image_name'];
$file_name_of_image = $image_returns['file_name'];
$p_id_of_image = $image_returns['p_id'];
$date_of_image = $image_returns['date'];
$file_type_of_image = $image_returns['file_type'];
$file_size_of_image = $image_returns['file_size'];

//Print data
echo $id_of_image."-".$name_of_image."-".$file_name_of_image."<br />";

}

 

The while loop is just going through one row of the results at a time. If you put the output outside of this loop, it is going to go through all of the records until it gets to the last one, exit the loop, then echo out the last row of data. So if you put the echo inside the loop, it will echo the data for each row.

 

 

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.