>我需要将数组从get_results()
传递到另一个查询调用以get_results()
我的代码是这样的:
$getids = get_results("Select ID from table_name");
$getData = get_results("Select name from table where ID In($getids)");
但是当我运行这段代码而不是$getids
它需要Array
由于$getids
是一个数组,如果你尝试将其作为字符串回显,你会得到单词Array
.相反,您需要implode
它以获得逗号分隔的值列表,即
$getData = get_results("Select name from table where ID In(" . implode(',', $getids) . ")");
但是,您可以通过在查询中使用JOIN
直接获取所需的数据:
$getData = get_results("SELECT name
FROM `table` t1
JOIN table_name t2 ON t2.ID = t1.ID");
$getids数组中。您需要将所有 id 作为逗号分隔的字符串传递,如下所示:
$getids = get_results("Select ID from table_name");
$getids = implode( ',', $getids );
$getData = get_results("Select name from table where ID In($getids)");
$getids是一个php数组,要在字符串变量中使用,你必须使用"implode"函数:
//first check array is not empty
if(count($getids))
{
//if your array is same as: $getids=array("ID"=>[1,2,3,4]) you must first convert it to flat array same as :$getids=[1,2,3,4]
// for example: $getids=$getids["ID"];
$sql="Select name from table where ID In(".implode(",",$getids).")";
}