PHP - Why while(mysql_fetch_array(mysql_query())) loops?


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.


  • rabbitmq-erlang-client, using rebar friendly pkg, works on dev env fails on rebar release
  • How can I override the .. and … operators of Ruby Ranges to accept Float::INFINITY?
  • Uncaught TypeError: Object [object Object] has no method 'fancybox'
  • Detecting Not-A-Number (NAN) or +-infinity in Access and Sql Server
  • Problem in PHP when joining tables having columns with the same name
  • Neo4j Recommendation Cypher Query Optimization
  • FOR /R - Using set /a [variable] = [variable] + %%~zA
  • json showing duplicate output of mysql result
  • static pre-calculation optimization in clang
  • What is the fastest way to select nearest geographical place from mysql database?
  • Different occurrences of a specific key in an array of hashes
  • what is for…in statement in javascript
  • Compare Pandas dataframes and add column
  • cURL not working while fetching Google API data
  • output json array in php
  • Web browser lock-down: How to?
  • Storing data from SQL in array
  • how to set id for each item in ListView
  • In Java, how can I construct a File from a resource?
  • Add dynamic data to line chart from mysql database with highcharts
  • bad substitution shell- trying to use variable as name of array
  • JBoss External Properties Files in Classpath
  • Unable to install Git-core+svn by MacPorts
  • Stop Bash Script if Hive Fails
  • Django simple Captcha “No module named fields” error
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • ActiveRecord query for a count of new users by day
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Could not find rake using whenever rails
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Launch Runnable Jar from Web Start
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • SQL merge duplicate rows and join values that are different
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Getting error when using KSoap library to consume .NET web services
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?