The fact is that in such a code $ result will access the database 6 times. How to assign a variable once, and use it?

for ($j = 1; $j < 7; $j++) { $result = mysql_query('SELECT * from teachers'); for ($y = 1; $y <= mysql_num_rows($result); $y++) { $myrow = mysql_fetch_array($result); echo '<option value = "' . $myrow['id'] . '">' . $myrow['Фамилия'] . '</option>'; } } 
  • Is it possible to somehow result from this piece of code for ($ y = 1; $ y <= mysql_num_rows ($ result); $ y ++) {$ myrow = mysql_fetch_array ($ result); echo '<option value = "'. $ myrow ['id']. '">'. $ myrow ['Last Name']. '</ option>'; } put in a variable and just output it? - Radik Kamalov
  • After all, essentially foreach ($ arr as $ row) {echo '<option value = "'. $ Row ['id']. '">'. $ Row ['Last name']. '</ Option>'; } will also be executed 6 times. - Radik Kamalov
  • 2
    why would you choose EVERYTHING from the table of teachers as many as SIX times? :) - Yakovlev Andrei

1 answer 1

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