参数化查询的示例

发布于 2024-08-14 10:23:36 字数 1566 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

倾城°AllureLove 2024-08-21 10:23:36

参数化查询本质上是一种抽象出所有输入的查询。这有几个好的副作用,例如使所有输入无害(即不可能进行有害注入)并使其在重复使用时速度更快,因为它是预先解析和编译的,因此引擎知道如何应用给定的输入。纯mysql中的一个例子是:

PREPARE qry FROM "INSERT INTO tbl VALUES (?)";

该语句现在已编译并缓存,并且可以重复执行,无需重新编译和解释它:

SET @var = "some input";
EXECUTE qry USING @var;
SET @var = "some other input";
EXECUTE qry USING @var;

在PHP中使用时,通常是这样的(缩写):

$stmt = prepare('INSERT INTO tbl VALUES(?)');
execute($stmt, array("some input"));
execute($stmt, array("some other input"));
execute($stmt, array("some more input"));

A parameterized query is essentially a query which abstracts away all the input. This has several good side effects, like making all input harmless (ie. no harmful injections are possible) and making it faster when used repeatedly, since it is pre-parsed and compiled, so the engine knows how to apply the input given. An example in pure mysql is:

PREPARE qry FROM "INSERT INTO tbl VALUES (?)";

The statement is now compiled and cached, and can be executed repeatedly without needing to recompile and interpret it:

SET @var = "some input";
EXECUTE qry USING @var;
SET @var = "some other input";
EXECUTE qry USING @var;

When used in PHP, it's usually like this (shortened):

$stmt = prepare('INSERT INTO tbl VALUES(?)');
execute($stmt, array("some input"));
execute($stmt, array("some other input"));
execute($stmt, array("some more input"));
苏辞 2024-08-21 10:23:36
PREPARE stmt_name FROM "SELECT name FROM Country WHERE code = ?";

SET @test_parm = "FIN";

EXECUTE stmt_name USING @test_parm;

来源:MySQL 开发:准备好的语句

PREPARE stmt_name FROM "SELECT name FROM Country WHERE code = ?";

SET @test_parm = "FIN";

EXECUTE stmt_name USING @test_parm;

Source: MySQL Dev: Prepared Statements

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