dbewick123 Posted March 13, 2013 Share Posted March 13, 2013 Hi, I'm trying to query my 'books' table to return all the tags (tag1 to tag5) that are in my 'userBooks' table (so basically if a user has that book in userBooks i want to get those tags)... hopefully by putting the code it should be clearer... $recommended = mysql_fetch_assoc(mysql_query("SELECT `tag1`, `tag2`, `tag3`, `tag4`, `tag5` FROM `books` WHERE `book_id` IN (SELECT `book_id` FROM `userBooks` WHERE `user_id` = '$sessionUserId')")); Just to note my userBooks tabe just has 2 foreign keys (user_id and book_id) and one primary key. the result i get when i print_r this is just the set of tags (1 to 5) for the first book_id that matches, my problem is that i want all the tags for all the book id's that match? any advise please? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/275609-problem-retrieving-data/ Share on other sites More sharing options...
Solution Barand Posted March 13, 2013 Solution Share Posted March 13, 2013 Don't nest mysql functions. You need a query followed by several fetches. $sql = "SELECT `tag1`, `tag2`, `tag3`, `tag4`, `tag5` FROM `books` INNER JOIN userbooks USING (book_id) WHERE `user_id` = '$sessionUserId' "; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { echo join (',', $row) . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/275609-problem-retrieving-data/#findComment-1418406 Share on other sites More sharing options...
dbewick123 Posted March 13, 2013 Author Share Posted March 13, 2013 Don't nest mysql functions. You need a query followed by several fetches. $sql = "SELECT `tag1`, `tag2`, `tag3`, `tag4`, `tag5` FROM `books` INNER JOIN userbooks USING (book_id) WHERE `user_id` = '$sessionUserId' "; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { echo join (',', $row) . '<br>'; } Thanks for this, don't worry if not but i would really appreciate it if you could explain what is happening here because it doesn't make a lot of sense to me :/ ? Quote Link to comment https://forums.phpfreaks.com/topic/275609-problem-retrieving-data/#findComment-1418415 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.