Jump to content

Problem retrieving data


Go to solution Solved by Barand,

Recommended Posts

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 
 

 

Link to comment
https://forums.phpfreaks.com/topic/275609-problem-retrieving-data/
Share on other sites

  • Solution

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>';
}
 

 

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 :/ ? 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.