使用函数的结果填充下拉列表

To fill a drop down list with the rsult of a function, I did the following:

function tree($root) { 
$n=0;
$result = $conn->prepare("SELECT id FROM personne WHERE emp_id='$root' ORDER BY     id"); 
$result->execute();
while ($row = $result->fetch(PDO::FETC_ASSOC)) {
$n = $n+1;
echo "<option>".$row['id']."</option>"; 
tree($row['id'], $n+1); 
}   
}

But I got this error:

Notice: Undefined variable: conn in C:\wamp\www\page.php on line 59

To get rid of the above problem, I made this change:

function tree($root, $connection) { 
$n=0;
$result = $connection->prepare("SELECT id FROM personne WHERE emp_id='$root' ORDER BY     id"); 
$result->execute();
while ($row = $result->fetch(PDO::FETC_ASSOC)) {
$n = $n+1;
echo "<option>".$row['id']."</option>"; 
tree($row['id'], $n+1); 
}   
}

Now, I'm facing this problem:

Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\page.php on line 59

Thanks for help

Your initial function was correct, it's just that the $conn variable is initiated outside of the function scope. To fix this, use global to bring it into context:

try this:

function tree($root) { 
global $conn;
$n=0;
$result = $conn->prepare("SELECT id FROM personne WHERE emp_id='$root' ORDER BY     id"); 
$result->execute();
while ($row = $result->fetch(PDO::FETC_ASSOC)) {
$n = $n+1;
echo "<option>".$row['id']."</option>"; 
tree($row['id'], $n+1); 
}   
}