fife Posted February 19, 2011 Share Posted February 19, 2011 I have a loop which works fine but before the loop I run a query which I store as $AlbumName From this array and within the loop I want to echo $AlbumName['album_name']; but obvisouly its not working. Can somebody please show me how to do it? <?php $qImage = "SELECT * FROM photos"; $rImage = mysql_query($qImage); $Array = mysql_fetch_array($rImage); $qAlbumName = "SELECT * FROM albums WHERE id =".$Array['album'].""; $rAlbumName = mysql_query($qAlbumName); $AlbumName = mysql_fetch_array($rAlbumName); while ($row_images = mysql_fetch_assoc($rImage) ) { ?> <?php echo $row_images['id']; echo $row_images['name']; echo $row_images['photo_description']; echo $AlbumName['album_name']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/ Share on other sites More sharing options...
silkfire Posted February 19, 2011 Share Posted February 19, 2011 What errors do you get? Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176722 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 I dont get any errors. It runs through the loop fine for all items called $row_image but ignores the $AlbumName. It just leaves it blank. Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176730 Share on other sites More sharing options...
Veteah Posted February 19, 2011 Share Posted February 19, 2011 There's a few things, for one mysql_fetch_assoc will return a numeric array unless you tell it not to be doing mysql_fetch_array($result, MYSQL_ASSOC) Other than that, your first sql query is selecting multiple rows but you're not looping the results. Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176733 Share on other sites More sharing options...
Skepsis Posted February 19, 2011 Share Posted February 19, 2011 First off, let's simplify your code and then check and see if we return data from the queries by dumping it. <?php // This part of the code is going to reuturn just ONE result, keep that in mind. // You may want to add, ORDER BY date DESC or something of that sort $imageq = mysql_query("SELECT * FROM photos"); $image = mysql_fetch_array($imageq); // This query is going to get the album name from whatever // image was returned, which is not set to a definite yet. $AlbumNameq = mysql_query("SELECT * FROM albums WHERE id = '{$image['album']}'"); $AlbumName = mysql_fetch_array($AlbumNameq); while ($row_images = mysql_fetch_assoc($imageq) ) { print_r($row_images); print_r($AlbumName); echo $row_images['id']; echo $row_images['name']; echo $row_images['photo_description']; echo $AlbumName['album_name']; } ?> The results we dump should output the array of keys and values that you want to use, this will help us figure out why it's not looping. Keep in mind that by doing this we execute a query twice, which is not necessary. For better code, we can do this instead: <?php // This part of the code is going to reuturn just ONE result, keep that in mind. // You may want to add, ORDER BY date DESC or something of that sort $imageq = mysql_query("SELECT * FROM photos ORDER BY id DESC"); while ($row_images = mysql_fetch_assoc($imageq) ) { // Set $row_images['album'] to however the album is defined in the photos table $AlbumNameq = mysql_query("SELECT * FROM albums WHERE id = '{$row_images['album']}'"); $AlbumName = mysql_fetch_array($AlbumNameq); echo $row_images['id']; echo $row_images['name']; echo $row_images['photo_description']; echo $AlbumName['album_name']; } ?> I strongly recommend using this second code and trying that, after fixing up whatever table values need to be corrected, if you need any explanations feel free to ask. Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176737 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 still the query comes back empty The $row_image loops perfectly but the $AlbumName is just blank. Ive echoed bother the queries and they are working. Its just once it loops it seems to ignore the $AlbumName Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176753 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 basically i have a photo table. With in that table there is an album ID. Im passing that on to the query to return the album name to display that as no one wants to see the id. Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176755 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 infact im wrong. If i try to echo echo $AlbumNameq = mysql_query("SELECT * FROM albums WHERE id = '{$row_rs_images['album']}'"); $AlbumName = mysql_fetch_array($AlbumNameq); nothing happens Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176759 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 Skepsis Thank you it works. The table is album not albums so your changes to my code were correct Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176762 Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 This is why it's a bad idea to have your query string formed in the query execution. Separate it, store it in a variable, and use the variable for the execution. Then you can properly debug the code. Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176763 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 can you explain please? Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176764 Share on other sites More sharing options...
Skepsis Posted February 19, 2011 Share Posted February 19, 2011 To me it feels like the issue is coming from the AlbumName query being used outside the loop, and the $array information being built off a bad query. Have you tried my second code from the previous example? that array query is going to return results if there is an album id that matches the one defined in the photos table. So say you loop the photos, you return stuff like: photo_id=1 photo_album=1 then the albums query, which is just meant to return ONE specific album, the name of the album that the picture is under obviously, so it makes sense your new album query will work. Go ahead and try the second code, but with this addition: <?php $images = mysql_query("SELECT * FROM photos ORDER BY id DESC"); while ($row = mysql_fetch_assoc($images) ) { // whatever the field is called in table photos that holds the album, change $row['album'] to that name $albums = mysql_query("SELECT * FROM albums WHERE id = '{$row['album']}'"); $album = mysql_fetch_array($albums); $row['album'] = $album['album_name']; echo $row['id']; echo $row['name']; echo $row['photo_description']; echo $row['album']; echo '<pre>'; print_r($row); print_r($album); echo '</pre>'; } ?> Tell me the dumps it provides, I want to know the query results for $row and $album. Edit: Sorry I didn't see that I had resolved your issue. Also, what pikachu2000 means is that you shouldn't have things like: $sql = "SELECT * FROM blah"; $sqlA = mysql_query($sql); You should just do, $sql = mysql_query("SELECT * FROM blah"); Please mark the topic as solved, and glad I could help. Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176770 Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 can you explain please? Form the query string outside mysql_query(), then when things inevitable crater, you have the ability to echo it in it's entirety. $query = "SELECT `field` FROM `table` WHERE `field` = $value"; if( !$result = mysql_query($query) ) { echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>'; } else { // query succeeded, continue. } Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176774 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 cool ok thank you for the advise. Both of you. I will change the way I do that from now on. Quote Link to comment https://forums.phpfreaks.com/topic/228195-looping-issue/#findComment-1176776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.