gpt4 book ai didi

php mysql查询结果数组和foreach循环

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:38 24 4
gpt4 key购买 nike

任何人都知道如何执行以下操作:

我有以下 mysql 查询:

客户端1026下有两个客户端:1056(左边)& 1497(右边)client 1056和client 1497下分别有两个client,以此类推

现在我想循环收集客户端1026下的所有客户端

我有这个 mysql 查询

$sql_query="select id from rev_r_clients WHERE parent_client_id='1026'";
$res = mysql_query($sql_query);
$ids=array();
while($row = mysql_fetch_object($res)){
$ids[]=$row->id;
}
$ids=array_filter($ids);
foreach($ids as $id){
echo $id;
echo '<br>';
$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
$res = mysql_query($sql_query);
$ids=array();
while($row = mysql_fetch_object($res)){
$ids[]=$row->id;
}
$ids=array_filter($ids);
foreach($ids as $id){
echo $id;
}
}

返回

1056

106410801497

15051521

现在我怎样才能让这个查询得到数组结果(比如 1056,1497),然后使用 foreach 循环得到结果和结果的结果等。直到没有更多的结果?

你可以使用 mysql , mysqli , PDO 我想要的只是完成请求

最佳答案

$ids=array();
while($row = mysql_fetch_object($res)){
ids[]=$row->id;
}
$ids=array_filter($ids);
/*To remove empty array*/
foreach($ids as $id){
echo $id;
}

关于php mysql查询结果数组和foreach循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20687828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com