在 PHP 中,PDO::PDOStatement->bindParam() 是否按预期工作?
我是第一次在 PHP 中尝试 PDO。我认为 PDOStatement->bindParam() 将是设置我传递给 sql 查询的值的数据类型的一种很酷的方法。但不知怎的,它对我不起作用。例如,我在 bindParam 调用中将变量的类型设置为 INT。但即使我将纯字符串值传递给它,它仍然不会抛出错误。也许我做错了什么。这是代码片段..
$query = "select * from PDO_TABLE where A_COLUMN = :test1 or B_COLUMN = :test2";
$test1 = '0';
$test2 = 'a';
$preparedStatement = $conn->prepare($query);
echo $preparedStatement->bindParam(':test1', $test1, PDO::PARAM_INT);
echo $preparedStatement->bindParam(':test2', $test2, PDO::PARAM_INT);
$preparedStatement->execute();
我做的一切正确吗?这难道不会引发参数 test2 的错误吗?
I am trying out PDO in PHP for the first time. I thought that PDOStatement->bindParam() would be a cool way to set the datatypes of the values that i pass in to the sql query. But somehow it doesnt work for me. For example I set the type of a variable to INT in bindParam call. But it still doesnt throw error even when I pass pure string values to it. Maybe I am doing something wrong. Here is the snippet of the code..
$query = "select * from PDO_TABLE where A_COLUMN = :test1 or B_COLUMN = :test2";
$test1 = '0';
$test2 = 'a';
$preparedStatement = $conn->prepare($query);
echo $preparedStatement->bindParam(':test1', $test1, PDO::PARAM_INT);
echo $preparedStatement->bindParam(':test2', $test2, PDO::PARAM_INT);
$preparedStatement->execute();
Am I doing everything properly? Isnt this supposed to throw error for the parameter test2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PDO 不会抛出异常或引发错误,但最多将参数转换为整数/长整型。
例如,在 pdo_stmt.c 中:
即,如果您已将参数注册为 PDO_INT 但该变量包含布尔值,PDO 会将 bool 转换为 int/long。
PDO won't throw an exception or raise an error but at best convert the parameter to an integer/long.
E.g. in pdo_stmt.c:
i.e. if you have registered a parameter as PDO_INT but the variable holds a boolean PDO converts the bool to an int/long.