0

我正在尝试嵌套两个 while 循环和一个 if 语句。

$Query = "SELECT * from lijst";  
$Query2 = "SELECT * from users";
$Result = mysql_query( $Query );  
$Result2 = mysql_query( $Query2 ); 

$Return = "<lijst>"; 
while ( $Lijst = mysql_fetch_object( $Result ))  
{ 

    $Return .= "<lijst>"; 
    while ($Users = mysql_fetch_object( $Result2 ))     
    { 
        if($Lijst['userid'] == $Users['userid'])
        {             
                        $Return .= "<username>".$Users->username."</username>"; 
        }

    }   
    $Return .= "<lijstid>".$Lijst->lijstid."</lijstid>"; 
    $Return .= "</lijst>";  
} 
$Return .= "</lijst>"; 

mysql_free_result($Result); 
mysql_free_result($Result2); 
print ($Return)

但是,我没有得到任何结果。我究竟做错了什么?

4

2 回答 2

0

你有两个错别字。

while ( $Users = mysql_fetch_object( $Result2 ) ) { if($Lijst['userid'] == $Users['userid') {

应该:

while ( $Users = mysql_fetch_object( $Result2 ) ) { if($Lijst['userid'] == $Users['userid']) {

print ($return)

错过最后的半列

print ($return);

除此之外,您的代码似乎有点混乱。以下不做任何事情:

$Return .= "";

以下是相同的代码,但没有混乱(变量名​​中的大写字母)。这使得编码和人们帮助你变得更容易。

$query   = "SELECT * from lijst";
$query2  = "SELECT * from users";

$result  = mysql_query($query);
$result2 = mysql_query($query2);
$return  = "";

while ($lijst = mysql_fetch_object($result)) {
    while ($users = mysql_fetch_object($result2)) {
        if ($lijst['userid'] == $users['userid']) {
            $return .= $users->username;
        }
    }
    $return .= "<lijstid>".$lijst->lijstid."</lijstid>"; 
}
mysql_free_result($result);
mysql_free_result($result2);
print ($return);
于 2013-01-03T14:26:28.577 回答
0

调用时mysql_fetch_object(),您应该将数据作为对象而不是数组访问

 if($Lijst['userid'] == $Users['userid'])

应该变成:

 if($Lijst->userid == $Users->userid)
于 2013-01-03T14:27:58.890 回答