这意味着什么?
我在谷歌搜索时在一些代码示例中发现了这一点:
$sql = 'INSERT INTO users (username,passwordHash) VALUES (?,?)';
这对我来说是新的,但我猜它是一种替代方法,相当于
$sql = "INSERT INTO users (username,passwordHash) VALUES ($username,$passwordHash)";`
或
$sql = 'INSERT INTO users (username,passwordHash) VALUES (' . $username . ',' . $passwordHash . ')';`
是否正确?这是一个实际的 PHP 语法,还是他只是想简化他的示例?
谢谢大家的反馈
I found this in some code examples while googling :
$sql = 'INSERT INTO users (username,passwordHash) VALUES (?,?)';
it's new to me, but I would guess that it a substitution method and equivalent to
$sql = "INSERT INTO users (username,passwordHash) VALUES ($username,$passwordHash)";`
or
$sql = 'INSERT INTO users (username,passwordHash) VALUES (' . $username . ',' . $passwordHash . ')';`
would that be correct? Is it an actual PHP syntax, or was he just trying to simplify his example?
Thanks for the feedback, folks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这在准备好的语句中很常见。
?
仅用作占位符,如下面的 PHP 文档所示:This is pretty common in prepared statements. The
?
merely serves as a placeholder, as seen below from the PHP documentation:问号是准备好的 SQL 语句中值的占位符 - 并且是针对 SQL 注入攻击的重要保护措施。除非每个用户都将其姓名括在引号中* 并且您将密码哈希括在引号中,否则您的第一个替代方案将无法正常工作。您的第二种选择很容易受到 SQL 注入攻击。
使用占位符,您可以在执行 SQL 时传递占位符的值。
* Tim O'Reilly 知道他确实必须输入“
'Tim O''Reilly'
”。The question marks are placeholders for values in prepared SQL statements - and are an important protection against SQL Injection Attacks. Your first alternative would not work properly unless every user encloses their name in quotes* and you enclose the password hash in quotes. Your second alternative is vulnerable to SQL Injection Attacks.
With placeholders, you pass the values for the placeholders when you execute the SQL.
* And Tim O'Reilly knows he really has to type "
'Tim O''Reilly'
".这不一样。问号用于准备好的语句查询。这些基本上允许您多次运行相同的查询,而只让系统解析查询一次。
it's not the same. question marks are used for prepared statement queries. these basically allow you to run the same query multiple times while only having the system parse the query once.