bindValu-6ren">
gpt4 book ai didi

php - PDO MySQL 如何处理预处理语句中的参数?

转载 作者:可可西里 更新时间:2023-11-01 08:35:43 26 4
gpt4 key购买 nike

例如,在我准备好的声明中的命名占位符中,我可以:

<?php
$stmt = $db->prepare("SELECT * FROM table WHERE id=:id AND name=:name");
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

->bindValuePDO::PARAM_INT 是否会使用“is_int< 检查 $id 是否真的是一个整数"或 "is_numeric"PHP 函数,或者以其他方式,如果 $id 不是 ->bindValue< ,它会失败并崩溃吗PDO::PARAM_INT 设置集期望它是?

我是 PDO MySQL 的新手,我也想知道:

$stmt->bindValue(':name', $name, PDO::PARAM_STR);

修复在 $name 中的任何编码问题,它是否也能自动处理修剪和条纹标签?

最佳答案

Will the ->bindValue with PDO::PARAM_INT check if the $id is really an integer using

它不会被检查。将变量绑定(bind)为 PARAM_INT 将使 PHP 简单地将其首先转换为整数 (int)"123"。没有错误发生,非数字字符串将简单地转换为零。另请参阅 String conversion to numbers 上的 PHP 手册.

... PDO::PARAM_STR);
Fix any encoding issues if accountered in $name, can it automatically deal trimming and striping tags as well?

对于字符串类型的参数,不会对传递的值进行自动修剪或转换。

如果输入字符集与数据库字符集不同,则编码将由 PDO 驱动程序或接收数据库服务器进行调整。但仅此而已。

关于php - PDO MySQL 如何处理预处理语句中的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com