为什么在mysql_query(“string”)中使用$query而不是直接查询字符串?
有人可以向我解释为什么以下内容不起作用:
mysql_query("DELETE FROM `categories` WHERE `id` = '{$id}'");
而以下内容确实有效:
$query = "DELETE FROM `categories` WHERE `id` = '{$id}'";
mysql_query($query);
Could someone please explain to me why the following doesn't work:
mysql_query("DELETE FROM `categories` WHERE `id` = '{$id}'");
Whereas the following does work:
$query = "DELETE FROM `categories` WHERE `id` = '{$id}'";
mysql_query($query);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它们的工作原理应该完全相同。
试试这个:
这对我总是有效。但有时将其放入变量中可以更轻松地使用。
They both should work exactly the same.
Try this:
That always works for me. But putting it in a variable makes it easier to work with sometimes.
两者没有区别。两者都容易受到 SQL 注入的攻击。您应该使用准备好的语句或
mysql_real_escape_string
。There's no difference between the two. Both are vulnerable to SQL injection, as well. You should use prepared statements or
mysql_real_escape_string
.两者之间没有功能差异,但将查询存储在单独的变量中可以更轻松地调试生成的语句。
例如
There's no functional different between the two, but storing the query in a separate variable makes it easier to debug the statement that's produced.
e.g.
我怀疑在线示例刚刚开始使用单独的
$query
变量,人们在没有真正考虑它的情况下复制了它,然后更多的示例实现了它,现在很多人都这样做,但不一定知道为什么。这就是生活。I suspect online examples just started using a separate
$query
variable, and people copied it without really thinking about it, and then more examples implemented that, and now tons of people do it without necessarily knowing why. C'est la vie.