PHP MySQL查询不起作用,但可在终端上起作用 [英] PHP MySQL Query doesn't work, but works from terminal

查看:80
本文介绍了PHP MySQL查询不起作用,但可在终端上起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

$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(); }

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

它可以从终端正常工作,并且可以正确打印$ gid,并且不会返回任何错误.我想念的东西真的很明显吗?

It works fine from the terminal, and it correctly prints out $gid, and no errors are returned. Am I missing something really obvious?

推荐答案

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

You are enclosing the query in single quotes. And in single quotes variable interpolation(also called substitution) does not happen.

简单的例子:

$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是整数,因此不需要在查询中将其括在引号中.因此,我们有:

Also from the line: $gid = (int) stripslashes($_POST['id']); its clear that $gid is an integer and there is not need to enclose it in quotes in your query. So we have:

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

这篇关于PHP MySQL查询不起作用,但可在终端上起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆