hansman Posted January 13, 2009 Share Posted January 13, 2009 Here is my code $query2 = "SELECT * From Songs where band_id = '$id'"; $result2 = mysql_query($query2) or die(mysql_error()); while($row2 = mysql_fetch_array($result2)){ $query = "SELECT * FROM Artists"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['bandname']. " - ". $row['yearformed']. " - ". $row['locationformed']. " - ". $row['summary']." ".$row['id']." - ". $row2['name'];; $id = $row['id']; print '<td><a href="'; print ($_SERVER['PHP_SELF']); print '?delete_id='.$id.'">Delete</a></td>'; print '</tr>'."\n"; }} ?> I have a table named "Songs" and "Artists" I have band_id in "Songs" and i would like every song by each band to come out next to it. What am I doing wrong? --I am connecting to the database fine-- Link to comment https://forums.phpfreaks.com/topic/140629-query-in-a-query/ Share on other sites More sharing options...
beyzad Posted January 13, 2009 Share Posted January 13, 2009 Here is my code $query2 = "SELECT * From Songs where band_id = '$id'"; $result2 = mysql_query($query2) or die(mysql_error()); while($row2 = mysql_fetch_array($result2)){ $query = "SELECT * FROM Artists"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['bandname']. " - ". $row['yearformed']. " - ". $row['locationformed']. " - ". $row['summary']." ".$row['id']." - ". $row2['name'];; $id = $row['id']; print '<td><a href="'; print ($_SERVER['PHP_SELF']); print '?delete_id='.$id.'">Delete</a></td>'; print '</tr>'."\n"; }} ?> I have a table named "Songs" and "Artists" I have band_id in "Songs" and i would like every song by each band to come out next to it. What am I doing wrong? --I am connecting to the database fine-- You must use this: $query = "SELECT * FROM Artists WHERE something='some values'"; Link to comment https://forums.phpfreaks.com/topic/140629-query-in-a-query/#findComment-735967 Share on other sites More sharing options...
redarrow Posted January 13, 2009 Share Posted January 13, 2009 $band_id will pull the current user with that band_id Take it away and try agin. Link to comment https://forums.phpfreaks.com/topic/140629-query-in-a-query/#findComment-735969 Share on other sites More sharing options...
redarrow Posted January 13, 2009 Share Posted January 13, 2009 consider a join then pull it out m8. Link to comment https://forums.phpfreaks.com/topic/140629-query-in-a-query/#findComment-735970 Share on other sites More sharing options...
RichardRotterdam Posted January 13, 2009 Share Posted January 13, 2009 something like: $sql="select * from songs left join bands on songs.band_id=bands.band_id"; Link to comment https://forums.phpfreaks.com/topic/140629-query-in-a-query/#findComment-736028 Share on other sites More sharing options...
hansman Posted January 13, 2009 Author Share Posted January 13, 2009 the above one works, but it will only display Artists that have songs! Link to comment https://forums.phpfreaks.com/topic/140629-query-in-a-query/#findComment-736401 Share on other sites More sharing options...
RichardRotterdam Posted January 13, 2009 Share Posted January 13, 2009 Do you mean not all songs have artists? If so you could use a right join instead of a left join Link to comment https://forums.phpfreaks.com/topic/140629-query-in-a-query/#findComment-736417 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.