这意味着什么?

发布于 2024-10-10 19:51:10 字数 468 浏览 3 评论 0原文

我在谷歌搜索时在一些代码示例中发现了这一点:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久光 2024-10-17 19:51:10

这在准备好的语句中很常见。 ? 仅用作占位符,如下面的 PHP 文档所示:

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();

This is pretty common in prepared statements. The ? merely serves as a placeholder, as seen below from the PHP documentation:

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
小嗷兮 2024-10-17 19:51:10

问号是准备好的 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'".

浅笑依然 2024-10-17 19:51:10

这不一样。问号用于准备好的语句查询。这些基本上允许您多次运行相同的查询,而只让系统解析查询一次。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文