Home > Article > Backend Development > PHP solves an infinite loop caused by compressed code_PHP tutorial
id | str1 | str2 |
---|---|---|
1 | saa | bbb |
2 | saa | bbc |
3 | sss | 123 |
<?php mysql_select_db("test", mysql_connect("localhost","******","******")); while($row = mysql_fetch_array(mysql_query("SELECT DISTINCT str1 FROM table1"))) { echo $row['str1']; echo "<br>"; } ?>
So I restored it step by step, and the infinite loop disappeared again. Obviously it is not a problem with the submission statement, but a problem with the grammatical structure.
Sure enough, the problem lies in while. Mysql_query will be executed every time a condition is detected. Then it was a mess.
Just make sure that mysql_query is executed only once. The correct code is as follows:
<?php mysql_select_db("test", mysql_connect("localhost","******","******")); $result = mysql_query("SELECT DISTINCT str1 FROM table1"); while($row = mysql_fetch_array($result)) { echo $row['str1']; echo "<br>"; } ?>