我正在尝试从数据库中获取数据然后使用不同的mysql_query在两个查询中使用while()再次获取它,但问题是它产生的结果不止一次,因为我在第一次查询时使用了。那么在没有while()的情况下获取所有数据的任何答案都可用于第一次查询?
$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'].'';
答案 0 :(得分:2)
你可以试试这个:
// Associative array
$frnd =mysql_fetch_array($getfrnd,MYSQL_ASSOC);
然后获取您的数据:
echo $frnd ["friend"]
echo $frnd ["followers"]
您应该使用mysqli_fetch_array
代替mysql_fetch_array
希望有所帮助
答案 1 :(得分:1)
您可以使用如下查询:
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
答案 2 :(得分:0)
您可以使用此功能轻松处理
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'];
}
}