如何动态使用mysqli bind_param [英] How to use mysqli bind_param dynamically

查看:61
本文介绍了如何动态使用mysqli bind_param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mysqli查询,其where子句在for循环中生成.因此参数在运行时之前是未知的.

I have a mysqli query whose where clause is generated in a for loop. So parameters are not known before runtime.

在这种情况下如何使用mysqli bind_param方法?

How can i use mysqli bind_param method in this case?

我可以在for循环中使用它来一一绑定参数吗?

Can i use it in the for loop to bind parameters one by one?

推荐答案

是的,使用php5.6是可能且非常简单的.首先,您需要知道有多少个参数str_repeat()count()可以提供帮助,以及拆包运算符( ...)也是如此,因此这种方法可以使绑定动态化.

Yes it's possible and very simple with php5.6, first you need know how many paraments str_repeat(), count() can you help and the unpacking operator (...) too, so this way is possible make binds dynimic.

$params = [10, 50, 51, 99];
$types = str_repeat('i',count($params));

$stmt = $mysqli->prepare("SELECT * FROM t WHERE id IN (?,?,?,?)");
$stmt->bind_param($types, ...$params);
if(!$stmt->excute()){
    echo mysqli_error($con);
}

这篇关于如何动态使用mysqli bind_param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆