-1

我对这个问题很着迷,我想从数据库中获取一个用逗号(字符串)分隔的数组,例如:(1,2,4,6),并将其作为变量插入到查询中,例如(从表中选择where id IN($variable string). 谁能帮我解决这个问题?

例子:

<?php
//connect
$sql = "select id from users where id=1";
//get the results as an array

在此查询中插入该变量

select from table where id IN($variable);
//
?>

我尝试了多种方法,但总是遇到不同的问题。请需要一个答案!

4

1 回答 1

1

implode()将数组转换为由您选择的字符分隔的值的字符串:

$arr = array(1,2,3,4,5,6,7,8,9); // make an array
$list = implode(',',$arr);
$sql = "SELECT * FROM table WHERE id IN($list)";
于 2013-05-09T19:47:20.230 回答