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 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need in getting data from db

    Hi
    i want to select data from db by comparing two things. First I want to select all the ids of currently logged in user's friends from friends table. then i want to select all the posts of currently logged in user's friends as well as his own posts from posts table. I am trying to achieve this task using following code but it does not work.
    Code:
    <?php 
    session_start();
    $connect = mysql_connect("localhost","user","pass");
    mysql_select_db("test");
    
    $friends = mysql_query("SELECT * FROM friend WHERE userid='$_SESSION[userid]'");
    
    while($friend = mysql_fetch_assoc($friends)) {
    
    $f = $friend['friendid'];
    //echo $f."<br />";
    
    $query = mysql_query("SELECT * FROM posts WHERE userid='$f' AND userid='$_SESSION[userid]'");
    
    while($id = mysql_fetch_assoc($query)) {
    $userpost = $id['post'];
    $date = $id['date'];
    echo $userpost."<br />".$date."<br />";
    }
    }
    ?>
    here are my friend and post tables
    Attached Thumbnails Attached Thumbnails need in getting data from db-friend.png   need in getting data from db-post.png  

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Thumbs up

    Hi,
    try this (single query instead 2) :
    PHP Code:
    $query "SELECT * FROM posts 
    WHERE userid = (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']}) "

    Regards

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by koko5 View Post
    Hi,
    try this (single query instead 2) :
    PHP Code:
    $query "SELECT * FROM posts 
    WHERE userid = (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']}) "

    Regards
    i tried this way
    Code:
    $friends = "SELECT * FROM posts 
    WHERE userid = (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']}) "; 
    $result = mysql_query($friends);
    echo $friends."<br><br>";
    while($friend = mysql_fetch_assoc($result)) {
    	
    		$userid = $friend['post'];
    		$date = $friend['date'];
    		echo $userid."<br />".$date."<br />";	
    	
    	
    }
    it is not working this way
    is this right if not please tell me how can i write it in correct way
    Last edited by doforumda; 02-09-2010 at 12:06 PM.

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    PHP Code:
    $friends "SELECT * FROM posts 
    WHERE userid = (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']}) "

    $result mysql_query($friends) or die('Error 1');
    while(
    $r mysql_fetch_assoc($result)) echo "{$r['post']}   {$r['date']}<br />"
    is it ok?

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by koko5 View Post
    PHP Code:
    $friends "SELECT * FROM posts 
    WHERE userid = (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']}) "

    $result mysql_query($friends) or die('Error 1');
    while(
    $r mysql_fetch_assoc($result)) echo "{$r['post']}   {$r['date']}<br />"
    is it ok?
    no it is not working it says Error 1

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    hmm try
    PHP Code:
    $result mysql_query($friends) or die(mysql_error()); 
    to get the exact error code...

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by koko5 View Post
    hmm try
    PHP Code:
    $result mysql_query($friends) or die(mysql_error()); 
    to get the exact error code...
    it displays subquery returns more than 1 row

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by doforumda View Post
    it displays subquery returns more than 1 row
    This should work:
    PHP Code:
    $friends "SELECT * FROM posts 
    WHERE userid IN (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']}) "

    $result mysql_query($friends) or die(mysql_error());
    while(
    $r mysql_fetch_assoc($result)) echo "{$r['post']}   {$r['date']}<br />"

  • #9
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by koko5 View Post
    This should work:
    PHP Code:
    $friends "SELECT * FROM posts 
    WHERE userid IN (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']}) "

    $result mysql_query($friends) or die(mysql_error());
    while(
    $r mysql_fetch_assoc($result)) echo "{$r['post']}   {$r['date']}<br />"
    it is still not working properly becoz it does not display posts of logged in user

  • #10
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    OK, replace $friends with:
    PHP Code:
    $friends "SELECT * FROM posts 
    WHERE userid={$_SESSION['userid']} OR userid IN (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']})"

    This will show currently logged-in user's posts+his friends posts.

  • #11
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by koko5 View Post
    OK, replace $friends with:
    PHP Code:
    $friends "SELECT * FROM posts 
    WHERE userid={$_SESSION['userid']} OR userid IN (SELECT friendid FROM friend 
    WHERE userid = {$_SESSION['userid']})"

    This will show currently logged-in user's posts+his friends posts.
    thanks man you are great

  • #12
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts
    My pleasure

    I've PM-ed you extended example, based on this code: hope you'll like it and it works, because I've appended few things in the pm-form directly.I'm sure that you'll fix it if I've missed any quote or bracket.
    Regards


  •  

    Posting Permissions

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