So I have the code:
$sql = "SELECT * from users WHERE level = 2";
$result = mysql_query($sql);
while($write = mysql_fetch_array($result)){
echo ''.$write['username'].'';
}
I want to make it more simple so I do:
while($write = mysql_fetch_array(mysql_query("SELECT * from users WHERE level = 2"))){
echo ''.$write['username'].'';
}
Why the first code isn't infinity looping and the second code is?
the first code iterates a resource given by mysql_query($sql);
the second iterates the query and loads the first row each time forever. so instead of going to the next row, it makes a new query and starts on that row.
As a side note - dont use mysql_*
functions. Use mysqli_
or pdo
instead.
It is because your query is inside which tells the while to execute the query again and again. If you are looking for a much simpler alternative, you can try using an ORM such phpactiverecord.