-2

I'm trying to do a recursive sql query, searching for all the children of a father and the children of that children, and so on.. The problem with the code below is that it only retrieves the first children tree.

  • I need to save all the emails of the entire tree in an array

    $hermanos = array();
    function obtener($id){
        $res = mysql_query("SELECT email,id FROM usuarios WHERE padre = '$id'", Conectar::con());
        while($row = mysql_fetch_assoc($res)){ 
            if ($row['email'] != 'waiting') {
                $hermanos[] = $row['email'];
                obtener($row['id']);
            }
        }
        return $hermanos;
    }
    $a = obtener($invitado);
    print_r($a);
    
Bryan Villafañe
  • 678
  • 3
  • 8
  • 18

1 Answers1

1

From the vague description I think you want another solution. This is called Hierchial data retireval according to db language. I think these kind of recursive queries should be done on db end with procedures/views You can check these links for heirchial data

  1. Sol1 with sql view

  2. Article about Hierarchical data in mysql

  3. another soln with view

Community
  • 1
  • 1
Gun2sh
  • 870
  • 12
  • 22