Php mysql\u查询返回错误的结果
考虑以下代码Php mysql\u查询返回错误的结果,php,mysql,Php,Mysql,考虑以下代码 if ( isset( $_SESSION['FBID'] ) ) { $uid = $_SESSION['FBID']; $sql = "SELECT *, count(member_nr) AS notifyMe FROM poolWinners WHERE member_nr = '$uid' AND notification ='1'";
if ( isset( $_SESSION['FBID'] ) ) {
$uid = $_SESSION['FBID'];
$sql = "SELECT *, count(member_nr) AS notifyMe
FROM poolWinners
WHERE member_nr = '$uid' AND notification ='1'";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
$notification = $row['notifyMe'];
}//while
if ( $notification > 0 ) {
echo '<span class="badge">' . $notification . '</span>';
} //if
var_dump($notification);
} //isset( $_SESSION['FBID'] )
上面的脚本返回一个成员有多少通知,如下图所示
我的问题
脚本返回的结果错误,通知数量错误。查看下表,表中的成员编号出现3次,因此:
$notification=$row['notifyMe']应该=3而不是1
我在这里错过了什么或做错了什么?感谢阅读使用
$sql = "SELECT *, count(*) AS notifyMe
FROM poolWinners
WHERE member_nr = '$uid' AND notification ='1'";
注意count*,它将获取符合条件的记录数
并初始化$notification=0;开始时。你试过从这个角度接近它吗
$sql = "SELECT * FROM poolWinners WHERE member_nr = '$uid' AND notification ='1'";
$result = mysql_query($sql);
$notification = array();
while($row=mysql_fetch_array($result)){
$notification[] = $row['notifyMe'];
}
//an array count on notification should give you the number of elements in the array i.e those that matched the query
$total_count = count($notification);
在您的代码中,通知总是一个,因为它只接受结果集中最后一行的notifyMe字段 如果要获取通知数,请尝试以下操作
if ( isset( $_SESSION['FBID'] ) ) {
$uid = $_SESSION['FBID'];
$sql = "SELECT *, count(member_nr) AS notifyMe
FROM poolWinners
WHERE member_nr = '$uid' AND notification ='1'";
$result = mysql_query($sql);
$notification = 0;
while($row=mysql_fetch_array($result)){
$notification++;
/*
OR $notification += $row['notifyMe'];
*/
}//while
if ( $notification > 0 ) {
echo '<span class="badge">' . $notification . '</span>';
} //if
var_dump($notification);
} //isset( $_SESSION['FBID'] )
谢谢,伙计,但我也遇到了同样的问题谢谢你的努力,但我不能为我的生活找出原因,但它仍然返回一个,即使编辑,我认为可以解决它,var_dump确认$notification值为1……任何其他建议定义$notification=0之前,同时还有相同的问题…:-猜测它返回到绘图板上,用于meEdit问题,并为表结构粘贴SQL如果您处理的是少量行,您可以尝试mysql_num_rows,以查看返回了多少行求和通知列中的值?因为它们看起来每个都是1,就像这样,从poolWinners中选择sumNot,其中member_nr='$uid'和notification=1;不需要对结果进行循环,直接获取总和,或者从poolWinners中选择countmember\u nr,其中member\u nr='$uid'和notification=1;并使用计数而不是总和,以避免通知值大于1。再次直接获取完整值,无需在结果集上循环
$sql = "SELECT *
FROM poolWinners
WHERE member_nr = '$uid' AND notification ='1'";
$result = mysql_query($sql);
$notification = mysql_num_rows($result);
$sql = "SELECT * FROM poolWinners WHERE member_nr = '$uid' AND notification ='1'";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
echo "Toal notification".mysqli_num_rows($result);
}
mysqli_close($con);
?>