PDO: quot;Invalid parameter numberquot; when substituting multiple parameters with same value(PDO:“无效的参数号用相同的值替换多个参数时)
问题描述
如果我的参数在查询中多次出现,我该如何绑定,如下所示?
How do I bind my parameter if it appears multiple times in the query as follows?
$STH = $DBH->prepare("SELECT * FROM $table WHERE firstname LIKE :string OR lastname LIKE :string");
$STH->bindValue(':string', '%'.$string.'%', PDO::PARAM_STR);
$result = $STH->execute();
推荐答案
您提到了准备语句的两个参数(同名),但您只为第一个参数提供了一个值(这就是错误所在).
You mentioned two parameters (with the same name) for the prepare statement, yet you supply a value for the first parameter only (that's what the error was about).
不太清楚 PDO 如何在内部解决相同参数名称的问题,但您总是可以避免这种情况.
Not quite sure how PDO internally solved the same-parameter-name issue, but you can always avoid that.
两种可能的解决方案:
$sql = "select * from $table ".
"where "
"first_name like concat('%', :fname, '%') or ".
"last_name like concat('%', :lname, '%')";
$stmt= $DBH->prepare($sql);
$stmt->bindValue(':fname', $string, PDO::PARAM_STR);
$stmt->bindValue(':lname', $string, PDO::PARAM_STR);
<小时>
$sql = "select * from $table ".
"where "
"first_name like concat('%', ?, '%') or ".
"last_name like concat('%', ?, '%')";
$stmt= $DBH->prepare($sql);
$stmt->bindValue(1, $string, PDO::PARAM_STR);
$stmt->bindValue(2, $string, PDO::PARAM_STR);
<小时>
顺便说一下,您现有的方式仍然存在 SQL 注入问题.
By the way, the existing way you have done still has SQL injection issues.
这篇关于PDO:“无效的参数号"用相同的值替换多个参数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PDO:“无效的参数号"用相同的值替换多个参数时
基础教程推荐
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
