July 24, 2013

mysql_fetch_assoc() returning single row even though there are many rows

Nanu146’s Question:

i am trying to run this program but unexpectedly the mysql_fetch_assoc() function is returning only the first entry even though there are many entries and further more in foreach loop error say that illegal ‘parameter’ username is given

$getQuery='SELECT * FROM userst  ORDER BY username';
foreach($rel as $Query[]){
echo $Query['username'];

$rel=mysql_fetch_assoc($result); will only fetch one row at a time depending on the resource pointer.

This pointer will increase once you use that statement, so using it with a loop statement will give you all the rows.

while ($rel = mysql_fetch_assoc($result)) {
    echo $rel['username'];

Please try more safer API’s like mysqli or PDO instead of mysql_* API. They are deprecated.


It is not recommended to use the old mysql extension for new development, as it has been deprecated as of PHP 5.5.0 and will be removed in the future.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!