Query Help. Rep for everyone who helps.

Discussion in 'PHP' started by Josh-H, Jul 7, 2007.

  1. #1
    Hello.

    First off here is the code:
    <? 
    include("include.php");
    
    $result = mysql_query("SELECT * FROM friends WHERE `of` = '$logged[username]'") 
    or die(mysql_error());  
    
    while($row = mysql_fetch_array($result)) {
    
    echo $row['friend'];
    	echo "<br />";
    
    $friends = $row['friend'];
    
    $result2 = mysql_query("SELECT * FROM users WHERE `username` = '$friends'") 
    or die(mysql_error());  
    
    }
    
    while($row2 = mysql_fetch_array($result2)) {
    
    	echo $row2['lname']. " - ". $row2['fname']. " - ". $row2['username'];
    	echo "<br />";
    
    }
    
    ?>
    PHP:
    The problem is row2 only returns anything if row finds only 1 friend. So I am guessing that the query of $row[friend] is merging all results instead of keeping them as for example "Dwight" "Josh" "Thomas" (separate) that it is putting them as "Dwight Josh Thomas" of course, this is not a result in the users table

    The idea is that it finds the name from the one table then goes to the users table for the rest of that users information such as first name, last name and avatar etc.

    If you don't understand, I will rexplain.

    Thank You.
     
    Josh-H, Jul 7, 2007 IP
    neroux likes this.
  2. ErsinAcar

    ErsinAcar Peon

    Messages:
    201
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?
    include("include.php");
    
    $result = mysql_query("SELECT * FROM friends WHERE `of` = '$logged[username]'")
    or die(mysql_error()); 
    
    while($row = mysql_fetch_array($result)) {
    
    echo $row['friend'];
        echo "<br />";
    
    $friends = $row['friend'];
    
    $result2 = mysql_query("SELECT * FROM users WHERE `username` = '$friends'")
    or die(mysql_error()); 
    
    while($row2 = mysql_fetch_array($result2)) {
    
        echo $row2['lname']. " - ". $row2['fname']. " - ". $row2['username'];
        echo "<br />";
    
    }
    }
    ?>
    PHP:
    u can try this?
     
    ErsinAcar, Jul 7, 2007 IP
  3. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #3
    personally i would use a join for something like this.

    
    <?php
        //connect shit here
        $sql = "select username, friend, avatar, url left join users on users.username = friends.friend where of = '".$logged['username']."'";
        $result = mysql_query($sql);
        while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
        {
            // echo data
        }
    ?>
    
    PHP:
     
    ansi, Jul 7, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    you get this solved?
     
    ansi, Jul 7, 2007 IP