这是我的代码:

$gid = (int) stripslashes($_POST['id']);
echo $gid;

$db = dbConnect(); $test = $db->query('update games set played = played + 1 where id = "$gid"'); echo $db->error; echo $db->errno; die(); }


从终端运行良好,正确打印$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 - PHP MySQL查询不起作用,但可从终端起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3775072/

10-13 04:18