PHP PDO:准备语句问题
是的。所以我使用 PDO lib 来连接数据库(MySQL)并与之通信
所以现在当我示例更新“bla”列的内容时它看起来像这样:
$sql = $connect->prepaer("UPDATE users SET bla='bla' WHERE id = $USER AND age =:age");
$sql->bindValue(":age", $age);
$sql->execute();
现在如果你可以看到我已经绑定了值:age only而不是$USER 。
我已经在所有其他查询中绑定了除 $USER 之外的所有其他值。
$USER 是您登录时使用的用户 ID。
我想知道我是否可以保护 $USER 变量,是否存在某种转义字符串,就像您可以使用 mysql_* (mysql_real_escape_string) 所做的那样?
否则我需要编辑所有查询,并添加 bindValue(:user, $USER)...
Yes. So I am using PDO lib to connect and communicate with the database (MySQL)
So now it looks like this when i example what to update "bla" column:
$sql = $connect->prepaer("UPDATE users SET bla='bla' WHERE id = $USER AND age =:age");
$sql->bindValue(":age", $age);
$sql->execute();
Now if you can see I have binded the value :age only and not $USER.
I have binded all my other values except $USER, in all my other queries too.
$USER is the user id you are logged in with.
I wonder if i can protect the $USER variable, if there like exists some kind of a escape string to this like you could do with mysql_* (mysql_real_escape_string) ?.
Else i would need to edit all my queries, and add bindValue(:user, $USER)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您可能可以使用任何可用的转义函数和方法,但问题是:为什么?当然,找到所有查询可能很麻烦,但我想说,半参数化查询确实很奇怪。请添加绑定即可;)
也就是说,mysql_real_escape_string() 有什么问题?如果您确实想要快速破解,只需将其添加到您的弦乐构建中即可演奏:)。
但同样,只需付出努力来添加绑定即可。请?未来的你会感谢你的。
Well, you probably can use any of the escape functions and methods available, but the question is: why? Sure, it might be a hassle to find all your querys, but i'd say it's a real strange thing to half-parameterize your queries. Just add the bind, please ;)
That said, what is the problem with mysql_real_escape_string()? If you're really going for the quick-hack, just add that to your string building and you can play :).
But again, just put in the effort to add the bind. please? Future you will thank you.
无论如何,改变用户以使其保持一致是值得的。这可能是额外的工作,但从长远来看这是值得的。
另外,以防万一您不知道 - 您可以将参数数组传递给 Execute 函数,而不是使用 bindValue()。我个人有一个包装数据库类,所以我的更新调用如下所示:
$db->Update("UPDATE users SET bla=? WHERE id=? AND Age=?", array('bla', $USER, Age ));
在我看来,看起来整洁多了。
然后在我的课堂上,我只是调用 SQL 上的准备和数组参数上的执行。
It's worth going through changing the user anyway to make it consistent. It may be extra work, but it'll be worth it in the long run.
Also, just incase you don't know - you can pass in an array of parameters to the Execute function rather than using bindValue(). I personally have a wrapper database class, so my update call looks like this:
$db->Update("UPDATE users SET bla=? WHERE id=? AND age=?", array('bla', $USER, age));
Looks much neater IMO.
Then in my class I'm just calling prepare on the SQL and Execute on the array parameter.
PDO:quote(),但你能做的最好的事情就是绑定这个值。
PDO:quote(), but the best you can do is binding this value too.