此查询在输入 phpMyAdmin 时有效,但我无法让它在 PHP 中工作:
SET @cnt = 0; UPDATE groupsnumber SET ordering = @cnt := (@cnt+1) ORDER BY name
这是我尝试过的代码:
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$resSrt =$mysqli->query("SET @cnt = 0;
UPDATE groupsnumber
SET ordering = @cnt := (@cnt+1) ORDER BY name");
if (!$resSrt)
{
echo "Error executing query: (" . $mysqli->errno . ") " . $mysqli->error;
}
这是错误消息:
Error executing query: (1064) 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 'UPDATE groupsnumber SET ordering = @cnt := (@cnt+1) ORDER BY name' at line 1
有什么想法可以解决这个问题吗?
最佳答案
简单的原因:
在
SET @cnt = 0; UPDATE groupsnumber SET ordering = @cnt:= (@cnt+1) ORDER BY name"
您正在尝试通过单个 $mysqli->query
函数执行 2 个查询。
SET
作为第一个查询更新
作为第二个查询
mysqli_
不允许通过单个 $mysqli->query 运行多个查询,这就是它安全且首选作为防止 sql_injections 的选项的原因。
将它们分为 2 个独立的查询,您就可以开始了! :)
关于php - 在php中传递变量sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23264586/