MoMoMajor Posted July 23, 2009 Share Posted July 23, 2009 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 More sharing options...
MatthewJ Posted July 23, 2009 Share Posted July 23, 2009 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 } Link to comment https://forums.phpfreaks.com/topic/167186-mysql_query-that-returns-multiple-rows/#findComment-881512 Share on other sites More sharing options...
MoMoMajor Posted July 23, 2009 Author Share Posted July 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/167186-mysql_query-that-returns-multiple-rows/#findComment-881521 Share on other sites More sharing options...
MatthewJ Posted July 24, 2009 Share Posted July 24, 2009 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. Link to comment https://forums.phpfreaks.com/topic/167186-mysql_query-that-returns-multiple-rows/#findComment-881750 Share on other sites More sharing options...
MoMoMajor Posted July 24, 2009 Author Share Posted July 24, 2009 thanks matthew for the reply. i fixed this problem a while ago. Link to comment https://forums.phpfreaks.com/topic/167186-mysql_query-that-returns-multiple-rows/#findComment-881755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.