[英]get_results("select * from table_name where id IN($array)") but it assumes $array as "Array"
I need to pass an array from get_results()
to another query call to get_results()
我需要将一个数组从
get_results()
传递给另一个对get_results()
查询调用
My code is like this:我的代码是这样的:
$getids = get_results("Select ID from table_name");
$getData = get_results("Select name from table where ID In($getids)");
But when I run this code Instead of $getids
it is taking Array
但是当我运行这段代码而不是
$getids
它正在使用Array
Since $getids
is an array, if you try to echo it as a string, you will get the word Array
.由于
$getids
是一个数组,如果您尝试将其作为字符串回显,您将得到单词Array
。 Instead, you need to implode
it to get a comma separated list of values ie相反,您需要对其进行内
implode
以获得逗号分隔的值列表,即
$getData = get_results("Select name from table where ID In(" . implode(',', $getids) . ")");
But you can get the data you want directly by using a JOIN
in your query:但是您可以通过在查询中使用
JOIN
直接获取您想要的数据:
$getData = get_results("SELECT name
FROM `table` t1
JOIN table_name t2 ON t2.ID = t1.ID");
$getids in an Array.数组中的 $getids。 You need to pass all ids as a comma-separated string like below:
您需要将所有 ID 作为逗号分隔的字符串传递,如下所示:
$getids = get_results("Select ID from table_name");
$getids = implode( ',', $getids );
$getData = get_results("Select name from table where ID In($getids)");
$getids is a php array, for use in string variables you must use "implode" function: $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).")";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.