本文介绍了在PDO中强烈键入参数的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将参数绑定到SQL语句时,可以提供像PDO::PARAM_STR这样的参数类型.如果不这样做,请键入默认值为PDO::PARAM_STR.专门设置每个参数类型的原因可能是什么? PDO :: PARAM_STR至少可以在MySQL中与任何参数一起使用.我认为,即使是PDO :: PARAM_STR,即使是BLOB列也可以使用.

When you bind parameters to SQL statement, you can provide parameter type like PDO::PARAM_STR. If you don't, type defaults to PDO::PARAM_STR. What can be the reasons to specifically set the type of each parameter? PDO::PARAM_STR works with any parameter as I know at least in MySQL. I think even with PDO::PARAM_STR can be used even with BLOB columns.

PDO :: PARAM_STR不会引入任何SQL注入,因为您仍具有准备好的查询.

PDO::PARAM_STR does not introduce any SQL injection because you still have prepared queries.

推荐答案

使用PARAM_STR总是可以在列值中使用,因为mySQL 将值隐式转换为可以的正确类型,但是例如在以下查询中它将失败:

Using PARAM_STR happens to always work in column values because mySQL implicitly converts values to the correct type where it can, but it will fail for example in this query:

$limit = 1;

$dbh->prepare("SELECT * FROM items LIMIT :limit");
$dbh->bindParam(":limit", $limit, PDO::PARAM_STR); 
     // Will throw "You have an error in your SQL syntax..."

在适当的情况下-应该绝对使用PARAM_INT-对于上述情况,并为可能期望更严格的mySQL以外的数据库引擎做准备.

one should absolutely use PARAM_INT where appropriate - for cases like the one above, and to prepare for database engines other than mySQL that may be more strict in what they expect.

这篇关于在PDO中强烈键入参数的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:24