Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts

    mysql_fetch_assos result one

    I'm having some difficulty with my mysql_fetch_assoc. Basically what I have is a user's post and i'm trying to print out a set of results that would show the user who's currently 'liking' their post (like facebook). However, there seems to be a problem that i'm not 100% sure how to fix just now and maybe you can help.

    Let's say my post id is "157", in my database the post "157" has 5 'likes' and the users id that like this post are "111, 2, 1, 114 and 42".

    With this code...

    PHP Code:
    $thispost $_REQUEST['id'];
             
             
    $grablikes mysql_query("SELECT `user_id`, `post_id` FROM `likes` WHERE `likes`.`post_id` = '$thispost'");
             
    $wholikes mysql_fetch_assoc($grablikes);
             
             echo 
    "$wholikes[user_id]"

    I get the result "111". Which is obviously not what i'm looking for. It's as if the fetch_assoc is not grabbing the other 4 results.

    I'm sure there is an easy way to solve this problem, hope you have time to help!

    Thanks!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    mysql_fetch_assoc() fetches one row at a time. Call it multiple times (a loop is best) to get multiple results.

    PHP Code:
    for ($i 0$i mysql_num_rows($grablikes); $i++)
    {
        
    $wholikes mysql_fetch_assoc($grablikes); 
        echo 
    $wholikes['user_id'];


  • Users who have thanked Fumigator for this post:

    brianjamesward (05-11-2011)

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    thats fantastic, worked a treat. I'm going to study into this method now!

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Is it possible to use the results of this inside a <a href='' title='$wholikes[user_id]'>link</a> ?

    only shows 1 when i try it like this a the moment

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    only shows 1 when i try it like this a the moment
    What you've tried?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    What you've tried?
    PHP Code:
    $thispost $_REQUEST['id'];
             
             
    $grablikes mysql_query("SELECT `user_id`, `post_id` FROM `likes` WHERE `likes`.`post_id` = '$thispost'");
             
             for (
    $i 0$i mysql_num_rows($grablikes); $i++)
                {
                     
    $wholikes mysql_fetch_assoc($grablikes); 
       
                     
    $users_name mysql_query("SELECT `username`, `id` FROM `users` WHERE `users`.`id` = '$wholikes[user_id]'");
                     
    $theirnames mysql_fetch_assoc($users_name);
     
               
                 } 
    Code:
    <a href='like.php?postid=$row[id]' class='someClass' title='$theirnames[username]'></a>
    just used that inside the link?
    Last edited by brianjamesward; 05-11-2011 at 09:30 AM.

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Don't you need to put your anchor inside the loop to get it repeated?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Don't you need to put your anchor inside the loop to get it repeated?
    anchor?

  • #9
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by brianjamesward View Post
    anchor?
    It's okay, ive just printed them out in a different way now. Thanks for all the help!

  • #10
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Quote Originally Posted by brianjamesward View Post
    anchor?
    http://www.w3.org/TR/html401/struct/links.html#h-12.1
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •