It is possible so for example:
$arr = array(); while($r = mysql_fetch_array($result)) { if($r == null) break; array_push($arr, $r); }
After that, in the $ arr array will be what you need (unless of course I correctly understood the question). Honestly, this is not a good solution. But I hope I got you on the right path.
To access the desired cell, you will need to write something like this:
$arr[0]['teacher_name']
That is, judging by your code, then you need to do the following:
$i = 0; while($i < count($arr)) { echo '<option value = "'.$arr[$i]['id'].'">'.$arr[$i]['Фамилия'].'</option>'; }
Or use foreach. He somehow looks prettier.
foreach ($arr as $row) { echo '<option value = "'.$row['id'].'">'.$row['Фамилия'].'</option>'; }
To put the result in a variable, use concatenation.
foreach($arr as $row) { $str .= '<option value="'.$row['id'].'">'.$row['Фамилия'].'</option>'; }