How to populate a dropdown list from array in FORM API
In a while looping over your result, populate array.

$user_list = array();
while ($row = $stmt-&gt;fetch()) { // See this line in your original code
   $user_list[$row-&gt;user_id] = $row-&gt;name; 
   // Apply transformations and conditions here, if needed
}


Or, as Clive suggested:

$user_list = $stmt-&gt;fetchAll(PDO::FETCH_KEY_PAIR);


Then simply use this array

'#options' =&gt; $user_list,




By the way, if you really want to make assignment in condition, proper way to write it is:

while ( ($row = $stmt-&gt;fetch()) )


As you can see, assignment is wrapped in double parentheses. It is the way to say future developers and code review tools "yes, it is not a typo, I really wanted it this way, I did not forgot about second =".