你如何在 mysqli 准备好的语句中使用 IN 子句

How do you use IN clauses with mysqli prepared statements(你如何在 mysqli 准备好的语句中使用 IN 子句)
本文介绍了你如何在 mysqli 准备好的语句中使用 IN 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用准备好的语句将一些旧代码移至新的 msqli 接口,我在处理包含 IN 子句的 SQL 语句时遇到问题.我通常会这样做:

I’m moving some old code over to the new msqli interface using prepared statements, I’m having trouble with SQL statements containing the IN clause. I would just normally do this:

$ids = '123,535,345,567,878'
$sql = "SELECT * FROM table WHERE id IN ($ids)";
$res = mysql_query($sql);

将其转换为 mysqli 和准备好的语句我尝试了多种解决方案:

Converting this to mysqli and prepared statements I have tried a number of solutions:

$ids = '123,535,345,567,878'
$ids = implode($ids,',');
$result = $msqli->prepare("SELECT foo,blar FROM table WHERE id IN (?));
$result->bind_param("i", $ids);
$result->execute();

以上失败,计算数组中的元素数和更改 SQL 字符串中问号的数量以及为数组中的每个元素调用 bind_parm 也失败.仅使用逗号分隔的字符串也会失败.

The above fails and calculating the number of elements in the array and altering number of question marks in the SQL string and calling bind_parm for each element in the array also fails. Just using the comma separated string also fails.

我在 Google 上找不到关于此的好的文档,那么您是如何解决这个问题的?

I can find no good documentation in Google on this, so how have you solved the problem?

推荐答案

看这里之前有人问过的一个类似问题的答案(第二个代码示例):

Look at the answer to a similar question that has been asked here before (second code sample):

我有一个整数数组,如何在 mysql 查询中使用每个整数(在 php 中)?

归结为:

  • 创建带有适量问号的 SQL 字符串
  • 使用 call_user_func_array() 将数组绑定到查询字符串
  • create the SQL string with the right amount of question marks
  • use call_user_func_array() to bind your array to the query string

这篇关于你如何在 mysqli 准备好的语句中使用 IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)