简体   繁体   English

MySQL 准备与 PHP mysqli 准备

[英]MySQL prepare vs PHP mysqli prepare

I was wondering if there was any difference between the MySQL PREPARE/EXECUTE clauses and the PHP mysqli prepare/execute methods?我想知道 MySQL PREPARE/EXECUTE 子句和 PHP mysqli prepare/execute 方法之间是否有任何区别? Are either better or worse at preventing injections?在预防注射方面是好是坏?

I am curious because I am writing a few database stored procedures and in one, the table and attributes are not known on compilation.我很好奇,因为我正在编写一些数据库存储过程,其中一个是编译时不知道表和属性。 I could write the data in as static, but the query is a bit complex and it would just bloat the procedure with a lot of control logic.我可以将数据写入为静态数据,但查询有点复杂,它只会使程序膨胀并包含大量控制逻辑。 It got me thinking about this though and I just wondered whether it is better if, when I just needed a simple statement, I write a short procedure with dynamic SQL or just prepare and bind with PHP.不过,这让我想到了这一点,我只是想知道,当我只需要一个简单的语句时,是使用动态 SQL 编写一个简短的过程,还是只准备并使用 PHP 进行绑定是否更好。

Also I apologize if this is a repeat and would of course welcome a link to an already answered SO question.如果这是重复,我也很抱歉,当然欢迎链接到已经回答的 SO 问题。 However, I looked generally on google and could not really find much in the way of a specific answer to this.但是,我一般在谷歌上查看,并没有真正找到对此的具体答案。

PHP mysqli is a layer, in PHP, around MySQL's prepare / execute functionality. PHP mysqli 是 PHP 中的一层,围绕 MySQL 的准备/执行功能。

Both will keep you safe from SQL injection as long as everything that comes in from users is stuffed into a bound variable.只要来自用户的所有内容都填充到绑定变量中,两者都会使您免受 SQL 注入的影响。

Prepared statements, handled either way, make for more efficient high-volume operation too.无论哪种方式处理的准备好的语句,也可以提高大容量操作的效率。 In MySQL the efficiency gain is modest compared to the high-priced DBMSs like Oracle, but it is still worth every bit of the trouble.在 MySQL 中,与像 Oracle 这样的高价 DBMS 相比,效率的提升是适度的,但它仍然值得付出每一点代价。

If you need to have table names as "variables" in your app, that's OK, You can't treat table names as bound variables though.如果您需要在应用程序中将表名作为“变量”,那没关系,但是您不能将表名视为绑定变量。 So you need to be totally paranoid about any user input that results in the construction of these table names.因此,您需要对导致构建这些表名的任何用户输入保持高度警惕。

As I answer it for 2020 and what I learned you should use to prepare and bind or bind inside execute with PDO, not with mysqli.正如我在 2020 年回答它以及我所学到的,您应该使用 PDO 而不是 mysqli 来准备和绑定或绑定内部执行。 It will be protected from injections and be safer.它将免受注射并且更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM