0

I am trying to get data from database then fetch it again with different mysql_query using while() in both query , but the problem it is producing results more than one time because i used while in first query . So any answer to get all data without while() for first query ?

$AllFrnd = "SELECT friend , followers FROM frndlist WHERE userid = '".$_SESSION[' user_id ']."' ORDER BY id DESC";
$getfrnd = mysql_query($AllFrnd);
while($frnd = mysql_fetch_array($getfrnd)) {
    $query2 = "SELECT * FROM post WHERE (userid ='".$frnd['friend']."') OR (userid =       '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC";
    $rs = mysql_query($query2);
    while($row = mysql_fetch_array($rs)) {
        echo ''.$row['content'].'';

3 Answers 3

2

you can try this :

// Associative array

   $frnd =mysql_fetch_array($getfrnd,MYSQL_ASSOC);

then get your data as like :

echo $frnd ["friend"]
echo $frnd ["followers"]

And you should use mysqli_fetch_array instead of mysql_fetch_array

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a query just like this :

SELECT *
FROM post
WHERE userid IN (
    SELECT followers
    FROM frndlist
    WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid IN (
    SELECT friend
    FROM frndlist
    WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid = '" . $_SESSION['user_id'] . "'
ORDER BY id DESC

Comments

0

You can easyly handle by using this function

function sel($table,$field="*", $condition="1",$sort="" ){
    if($sort!='') $sort="order by $sort ";
    //echo "select $field  from $table where $condition $sort ";
    $sel_query=mysql_query("select $field  from $table where $condition $sort ");
    //$sel_result=array();
    while($temp_res=@mysql_fetch_array($sel_query))
    {
        $sel_result[]=$temp_res;
    }
    return isset($sel_result)?$sel_result: 0;
}


while($frnd = mysql_fetch_array($getfrnd)) {
    $temp_res=sel("post","*"," (userid ='".$frnd['friend']."') OR (userid =       '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC");
    if($temp_res)foreach($temp_res as $row){
        echo $row['content'];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.