我使用的是最新版本的 PHP 5 和 MySQL。
查询如下:
$sql = "SELECT max(num)
FROM list
WHERE value = ?";
如果我手动将该值替换为值:$my_value
并从 phpMyAdmin 执行查询,我将获得正确的结果。
相反,如果我以这种方式准备查询,我不会获得任何内容:
$stmt = $con->prepare($sql);
$stmt->bind_param("s", $my_value);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($num);
$stmt->close();
echo "Num: [" . $num . "]";
输出为:Num:[]
最佳答案
bind_result()
不会消除调用 fetch()
命令的需要。您的代码应类似于:
$stmt = $con->prepare($sql);
$stmt->bind_param("s", $my_value);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($num);
$stmt->fetch();
$stmt->close();
echo "Num: [" . $num . "]";
请参阅docs了解更多信息。
关于php - 无法在准备好的 mysql 查询中使用 MAX(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45442438/