我可以使用MYSQLI和PHP将多个值绑定为单个参数吗? [英] Can I bind multiple values as a single parameter using MYSQLI and PHP?

查看:38
本文介绍了我可以使用MYSQLI和PHP将多个值绑定为单个参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下我有以下SQL查询:

Imagine I have the following SQL query:

SELECT id,name FROM user WHERE id IN ('id1','id2','id3')

现在想象一下,我需要由PHP提供的ID数组.所以我有这样的东西:

Now imagine I need the array of ids to be supplied by PHP. So I have something like this:

$idList = array('id1','id2','id3');
$query = "SELECT id,name FROM user WHERE id IN (?)";
$stmt = $db->prepare($query);
$stmt->bind_param(/*Something*/);

我可以用什么替换/*Something*/以获得与原始查询相同的结果?还是我需要在查询格式中放入3个问号?我不想这样做的唯一原因是因为问号的数量是可变的,所以我必须手动构建查询字符串.

What can I replace /*Something*/ with to get the same results as the original query? Or do I need to put in 3 question marks in the query format? The only reason I don't want to do that is because the number of question marks is variable, so I would have to build the query string manually.

推荐答案

我可以将多个值绑定为一个值吗? 使用MYSQLI和PHP设置参数?

Can I bind multiple values as a single parameter using MYSQLI and PHP?

不,你不能.

根据您的情况,应以编程方式构建查询字符串.如果可以保证始终是三个值,则可以在SQL中添加三个标记,然后通过遍历数组进行绑定.

For your situation, you should build the query string programmatically. If you are guaranteed it will always be three values, you could add three markers to the SQL then bind via looping over the array.

这篇关于我可以使用MYSQLI和PHP将多个值绑定为单个参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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