問題描述
當您將參數綁定到 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.
推薦答案
Using 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 中強類型參數的原因?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!