简体   繁体   中英

php mysql query result array and foreach loop

Any one have idea how to do the following:

I have the following mysql query :

client 1026 has two clients under him: 1056 (on the left ) & 1497 ( on the right ) and each of client 1056 and client 1497 has two other clients under them, and so on

now i want to make a loop to collect all the clients under the client 1026

i have this mysql query

$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;
    }
}

it return

1056

106410801497

15051521

now how can i make this query get array result ( like 1056,1497 ) and then use foreach loop to get the result and the result of the result of the result etc.. until there is no more result?

You can use mysql , mysqli , PDO all i want is to accomplish the request

$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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM