php多个参数绑定,php – 多次使用绑定参数

我正在为数据库实现一个漂亮的基本搜索引擎,用户可能会在其中包含不同类型的信息。搜索本身由几个联合组成,选择结果总是合并到3列中。

然而,返回的数据正在从不同的表中获取。

每个查询使用$ term进行匹配,我将其绑定到“:term”作为准备参数。

现在手册说:

You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name twice in a prepared statement.

我想,而不是用termX(x为term = n)替换每个:term参数,必须有一个更好的解决方案?

或者我只需要绑定X号码:termX?

编辑发布我的解决方案:

$query = "SELECT ... FROM table WHERE name LIKE :term OR number LIKE :term";

$term = "hello world";

$termX = 0;

$query = preg_replace_callback("/\:term/", function ($matches) use (&$termX) { $termX++; return $matches[0] . ($termX - 1); }, $query);

$pdo->prepare($query);

for ($i = 0; $i < $termX; $i++)

$pdo->bindValue(":term$i", "%$term%", PDO::PARAM_STR);

好的,这里是一个例子。我没有时间为sqlfiddle,但如果有必要,我会添加一个。

(

SELECT

t1.`name` AS resultText

FROM table1 AS t1

WHERE

t1.parent = :userID

AND

(

t1.`name` LIKE :term

OR

t1.`number` LIKE :term

AND

t1.`status` = :flagStatus

)

)

UNION

(

SELECT

t2.`name` AS resultText

FROM table2 AS t2

WHERE

t2.parent = :userParentID

AND

(

t2.`name` LIKE :term

OR

t2.`ticket` LIKE :term

AND

t1.`state` = :flagTicket

)

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值