gpt4 book ai didi

当查询具有 mysql 用户变量时,PHP PDO/MySQLi 不返回行

转载 作者:行者123 更新时间:2023-11-29 17:36:35 24 4
gpt4 key购买 nike

我想执行并获取以下查询的结果:

$query = <<<SQL
set @num := 0, @priority := '';
select * from (
select
id, status_ts,
@num := if(@priority = priority, @num + 1, 1) as _row_number,
@priority := priority as priority
FROM ($priority_query) as get_priority
ORDER BY priority DESC, status_ts ASC
) as items where items._row_number <= CEIL(priority);
SQL;

$sql = $PDO->query($query); $sql->rowCount() 返回 0,没有结果行。我已经通过直接在数据库中执行查询来测试该查询并且它有效。

最佳答案

解决方法是进行多个查询,将 set 更改为 select,然后正确迭代结果。

$query = <<<SQL
select @num := 0, @priority := '';
select * from (
select
id, status_ts,
@num := if(@priority = priority, @num + 1, 1) as _row_number,
@priority := priority as priority
FROM ($priority_query) as get_priority
ORDER BY priority DESC, status_ts ASC
) as items where items._row_number <= CEIL(priority);
SQL;

PDO

    $sql = $pdo->query($query);

if ($sql && $sql->nextRowset()) {
$items = [];
$numRows = $sql->rowCount();
if (($numRows > 0)) {
$items = $sql->fetchAll(\PDO::FETCH_OBJ);
}
} else {
$error = $this->DB->pdo->errorInfo();
throw new \Exception($error[2]);
}

MySQLi

$mysqli->multi_query($query);
$mysqli->next_result();

if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
}

关于当查询具有 mysql 用户变量时,PHP PDO/MySQLi 不返回行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50235514/

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