dsqe46004 2017-08-07 00:06
浏览 33
已采纳

MySQL Count不填充变量

Problem is: I want to count the number in my column r_start where the r_start dates are between my other two variables. But it always gives me the Error Notice: Undefined variable: z in [path] on line 173. So it doesn't fill the variable, what makes me think that the COUNT does not work porperly. I did a lot of reasearch and tried some things, but I simply don't find the cause..

$sqld = "SELECT r_start, COUNT (*) FROM reservation WHERE r_start BETWEEN '".$cid."' and '".$cod."' GROUP BY r_start";
if ($result = $con->query($sqld)) {
    $z = mysqli_fetch_assoc($result);
}

This error only appears with the COUNT tag. In my other queries it works absolutly fine. f.e.:

$sqlc = "SELECT * FROM reservation where r_ende between '".$cid."' and '".$cod."'";
if ($result = $con->query($sqlc)) {
    $y = mysqli_fetch_assoc($result);
}

Can anybody tell my why? Have I done something wrong?

  • 写回答

1条回答 默认 最新

  • doutan5844 2017-08-07 00:07
    关注

    MySQL generally does not recognize a space between a function and the opening paren. I would also advise that you give the column an alias:

    SELECT r_start, COUNT(*) as cnt
    FROM reservation r
    WHERE r_start BETWEEN '".$cid."' and '".$cod."'
    GROUP BY r_start;
    

    You should also learn to use parameters to pass values into queries. Not using parameters makes the code subject to unexpected (and hard-to-debug) syntax errors, as well as making it vulnerable to SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿