gpt4 book ai didi

php - 绑定(bind)参数时MySQLi语法错误

转载 作者:行者123 更新时间:2023-11-29 10:52:51 26 4
gpt4 key购买 nike

您好,我正在努力显示特定时间段内的事件。代码如下。当我在不绑定(bind)参数的情况下运行查询时它可以工作,但这样做给我带来了一些问题。代码如下:

$category = $_GET['category'];
$time = $_GET['time'];
$time_ahead = strtotime("+1 month", $time);

$query = "SELECT * FROM happenings ";
$query .= "WHERE ((date_start > ? AND date_start < ?) OR (date_start < ? AND date_end > ?) OR (date_end > ? AND date_end < ?))";
if($category)
$query .= " AND category = ?";
$query .= " ORDER BY date_start ASC";

$stmt = $mysqli->prepare($query) or die ("Could not prepare statement:" . $mysqli->error);

if($category)
$stmt->bind_param('sssssss', $time, $time_ahead, $time, $time_ahead, $time, $time_ahead, $category) or die("Could not bind parameters statement:" . $stmt->error);
else
$stmt->bind_param('ssssss', $time, $time_ahead, $time, $time_ahead, $time, $time_ahead) or die("Could not bind parameters statement:" . $stmt->error);

$stmt->execute() or die("Execute failed: (" . $stmt->errno ." ". $stmt->info .") " . $stmt->error);
$result = $stmt->get_result();

我收到的错误是:

Could not run the query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND date_start < ?) OR (date_start < ? AND date_end > ?) OR (' at line 1

当我打印出查询时,我得到了这个

SELECT * FROM happenings WHERE ((date_start > ? AND date_start < ?) 
OR (date_start < ? AND date_end > ?) OR (date_end > ? AND date_end < ?))
ORDER BY date_start ASC

现在已经有一段时间了,但我想不出语法错在哪里。没有绑定(bind)就运行得很好,那么我还需要做什么吗?我不相信我使用任何保留关键字等。我感谢任何帮助!

最佳答案

郑重声明,答案是在上面的评论中猜测的。

代码意外调用了 $mysqli->query() 而不是 $mysqli->prepare()

query() 方法无法识别参数占位符语法。

关于php - 绑定(bind)参数时MySQLi语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43442475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com