查询不正确

时间:2015-01-26 01:30:30

标签: php mysql

我有2个不同的表,我从中获取数据。第一个表是Users,第二个表是mob,就像朋友一样。好吧,我从mob行中限制了9行并从Users表中获取了他们的信息。它们应按收入* 10排序,但它们按照随机顺序排列。这是编码:

<?php
$get_mob = mysql_query("SELECT sent_id FROM mob WHERE sender_id=".$id." LIMIT 9");
while($mob_info = mysql_fetch_array($get_mob)) {
$mob_id = $mob_info['sent_id']; 
// Get mob stats
$get_users = mysql_query("SELECT id,username,image,income,last_login FROM users WHERE id=".$mob_id." ORDER BY income");
while($user_info = mysql_fetch_array($get_users)) {
$user_id = $user_info['id'];    
$user_name = $user_info['username'];    
$user_image = $user_info['image'];  
$user_income = $user_info['income'];    
$user_bounty = $user_income * 10;
$user_bonus = $user_bounty / 2;
$user_login = $user_info['last_login']; 
}
echo 
'<div id="MyMobBlock">
<div id="MyMobName"><span title="'.$user_name.'">'.substr($user_name,0,10).'..</span></div>
<div align="center"><img src="'.$user_image.'" width="60" height="60" /></div>
<div class="Success" align="center" title="$'.number_format($top_collect).'">$'.number_format($user_bonus).'</div>
</div>';    
}
?>

这里有一张照片,正如你所看到的那样,他们根本没有按顺序排列,而且他们的收入高于朋友列表。

Out of order and not showing the correct users

那么,有什么方法可以纠正这种情况并按收入顺序显示合适的人?

2 个答案:

答案 0 :(得分:1)

试试这个:

$get_users = "select b.id as id, b.username as username, b.image as image, b.income as income, b.last_login as last_login from mob a inner join users b on a.sent_id = b.id WHERE a.sender_id=".$id." ORDER BY b.income LIMIT 9";

while($user_info = mysql_fetch_array($get_users)) {
$user_id = $user_info['id'];    
$user_name = $user_info['username'];    
$user_image = $user_info['image'];  
$user_income = $user_info['income'];    
$user_bounty = $user_income * 10;
$user_bonus = $user_bounty / 2;
$user_login = $user_info['last_login']; 

echo 
'<div id="MyMobBlock">
<div id="MyMobName"><span title="'.$user_name.'">'.substr($user_name,0,10).'..</span></div>
<div align="center"><img src="'.$user_image.'" width="60" height="60" /></div>
<div class="Success" align="center" title="$'.number_format($top_collect).'">$'.number_format($user_bonus).'</div>
</div>';    
}

答案 1 :(得分:0)

您的回显不在有序查询的循环中:

while($user_info = mysql_fetch_array($get_users)) {
$user_id = $user_info['id'];    
$user_name = $user_info['username'];    
$user_image = $user_info['image'];  
$user_income = $user_info['income'];    
$user_bounty = $user_income * 10;
$user_bonus = $user_bounty / 2;
$user_login = $user_info['last_login']; 
}
echo 
'<div id="MyMobBlock">
<div id="MyMobName"><span title="'.$user_name.'">'.substr($user_name,0,10).'..</span></div>
<div align="center"><img src="'.$user_image.'" width="60" height="60" /></div>
<div class="Success" align="center" title="$'.number_format($top_collect).'">$'.number_format($user_bonus).'</div>
</div>';

对于SELECT id,username,image,income,last_login FROM users WHERE id=".$mob_id." ORDER BY income语句的每个结果(未订购),您只回显SELECT sent_id FROM mob WHERE sender_id=".$id." LIMIT 9语句的最后行。

另外,请考虑使用mysqli_而不是mysql_。此外,您可能希望使用预准备语句(特别是对于您反复执行的语句)。

更新:由于您在理解自己的代码时遇到了问题,因此这相当于:

<?php
//You're selecting 9 mobs, NOT ORDERING BY ANYTHING...
$get_mob = mysql_query("SELECT sent_id FROM mob WHERE sender_id=".$id." LIMIT 9");
while($mob_info = mysql_fetch_array($get_mob)) {
    $mob_id = $mob_info['sent_id']; 
    //For each of the 9 mobs, you're only getting the last result of this query:
    $user_info = mysql_query("SELECT id,username,image,income,last_login FROM users WHERE id=".$mob_id." ORDER BY income")->get_last_result(); //Gets the last result form this query
    //Which is the user with the least income for that particular mob

    $user_id = $user_info['id'];    
    $user_name = $user_info['username'];    
    $user_image = $user_info['image'];  
    $user_income = $user_info['income'];    
    $user_bounty = $user_income * 10;
    $user_bonus = $user_bounty / 2;
    $user_login = $user_info['last_login'];

    //You're outputting this last result (the user of this mob with the least income, for THAT SPECIFIC mob)
    echo 
    '<div id="MyMobBlock">
    <div id="MyMobName"><span title="'.$user_name.'">'.substr($user_name,0,10).'..</span></div>
    <div align="center"><img src="'.$user_image.'" width="60" height="60" /></div>
    <div class="Success" align="center" title="$'.number_format($top_collect).'">$'.number_format($user_bonus).'</div>
    </div>';
}
//And you didn't accomplish what you wanted.
?>