如何使用PHP数组与SQL IN操作符? [英] How to use php array with sql IN operator?

查看:592
本文介绍了如何使用PHP数组与SQL IN操作符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和两个数值数组,我想选择查询与SQL使用它IN操作符。

下面是我的表结构

  ID comp_id
1 2
2 3
3 1

我有一个数组 $改编这有两个值阵列([0] => 1 [1] => 2)

我想取comp_id 1的记录和comp_id 2。所以我写了下面的查询。

  SELECT * FROM表,其中comp_id IN($ ARR)

不过,这并不返回结果。


解决方案

  $ SQL =SELECT * from表where comp_id IN(.implode('',$ ARR)。 );

I have and array with two values and I want to use it with sql IN operator in select query.

Here is the structure of my table

id comp_id
1   2
2   3
3   1

I have an array $arr which have two values Array ( [0] => 1 [1] => 2 )

I want to fetch the record of comp_id 1 and comp_id 2. So I wrote the following query.

SELECT * from table Where comp_id IN ($arr)

But it does not return the results.

解决方案

$sql = "SELECT * from table Where comp_id IN (".implode(',',$arr).")";

这篇关于如何使用PHP数组与SQL IN操作符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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