gpt4 book ai didi

PHP MySQL 查询不起作用,但可以从终端运行

转载 作者:行者123 更新时间:2023-11-29 01:47:14 26 4
gpt4 key购买 nike

这是我的代码:

$gid = (int) stripslashes($_POST['id']);
echo $gid;<p></p>

<p>$db = dbConnect();
$test = $db->query('update games set played = played + 1 where id = "$gid"');
echo $db->error;
echo $db->errno;
die();
}
</p>
它在终端上工作正常,并且正确打印出 $gid,并且没有返回任何错误。我是否漏掉了一些非常明显的东西?

最佳答案

您将查询括在单引号中。并且在单引号中变量插值(也称为替换)不会发生。

简单的例子:

$who = 'harry potter';
echo 'hi "$who"'; // prints hi "$who"
echo "hi '$who'"; // prints hi 'harry potter'

将您的代码更改为:

$test = $db->query("update games set played = played + 1 where id = '$gid'");

同样来自行:$gid = (int) stripslashes($_POST['id']); 很明显 $gid 是一个整数并且有不需要在查询中将其括在引号中。所以我们有:

$test = $db->query("update games set played = played + 1 where id = $gid");

关于PHP MySQL 查询不起作用,但可以从终端运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3775072/

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