Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-11-2011, 08:38 AM   PM User | #1
brianjamesward
New Coder

 
Join Date: Feb 2011
Posts: 64
Thanks: 9
Thanked 0 Times in 0 Posts
brianjamesward is an unknown quantity at this point
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!
brianjamesward is offline   Reply With Quote
Old 05-11-2011, 08:57 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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'];

__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
brianjamesward (05-11-2011)
Old 05-11-2011, 08:59 AM   PM User | #3
brianjamesward
New Coder

 
Join Date: Feb 2011
Posts: 64
Thanks: 9
Thanked 0 Times in 0 Posts
brianjamesward is an unknown quantity at this point
thats fantastic, worked a treat. I'm going to study into this method now!
brianjamesward is offline   Reply With Quote
Old 05-11-2011, 09:07 AM   PM User | #4
brianjamesward
New Coder

 
Join Date: Feb 2011
Posts: 64
Thanks: 9
Thanked 0 Times in 0 Posts
brianjamesward is an unknown quantity at this point
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
brianjamesward is offline   Reply With Quote
Old 05-11-2011, 09:27 AM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
only shows 1 when i try it like this a the moment
What you've tried?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 05-11-2011, 09:28 AM   PM User | #6
brianjamesward
New Coder

 
Join Date: Feb 2011
Posts: 64
Thanks: 9
Thanked 0 Times in 0 Posts
brianjamesward is an unknown quantity at this point
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..
brianjamesward is offline   Reply With Quote
Old 05-11-2011, 09:53 AM   PM User | #7
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Don't you need to put your anchor inside the loop to get it repeated?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 05-11-2011, 10:26 AM   PM User | #8
brianjamesward
New Coder

 
Join Date: Feb 2011
Posts: 64
Thanks: 9
Thanked 0 Times in 0 Posts
brianjamesward is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
Don't you need to put your anchor inside the loop to get it repeated?
anchor?
brianjamesward is offline   Reply With Quote
Old 05-11-2011, 10:43 AM   PM User | #9
brianjamesward
New Coder

 
Join Date: Feb 2011
Posts: 64
Thanks: 9
Thanked 0 Times in 0 Posts
brianjamesward is an unknown quantity at this point
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!
brianjamesward is offline   Reply With Quote
Old 05-11-2011, 12:01 PM   PM User | #10
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Originally Posted by brianjamesward View Post
anchor?
http://www.w3.org/TR/html401/struct/links.html#h-12.1
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:28 AM.


Advertisement
Log in to turn off these ads.