-3

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

<?php
$dbc=mysql_connect("localhost","root","nanu");
mysql_select_db('users');
$getQuery='SELECT * FROM userst  ORDER BY username';
$result=mysql_query($getQuery,$dbc);
$rel=mysql_fetch_assoc($result);
echo'<pre>'.print_r($rel).'<pre/>';
echo"<br/>".$rel['username']."<br/>";
foreach($rel as $Query[]){
echo $Query['username'];
}
?>
nanu146
  • 97
  • 1
  • 1
  • 9
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Jul 24 '13 at 12:04
  • 1
    -1 because you didn't read the manual. Or one of trillion tutorials. – N.B. Jul 24 '13 at 12:04
  • Im still wondering on how hard did you try to trace the problem, probably the first google results provides an example of the usage of the mysql_fetch_assoc. – Jonathan Römer Jul 24 '13 at 12:04

5 Answers5

5

mysql_fetch_assoc() always returns the next row or false. You should call it in a loop:

while ($rel = mysql_fetch_assoc($result)) {...}
Greg
  • 9,068
  • 6
  • 49
  • 91
Gavriel
  • 18,880
  • 12
  • 68
  • 105
2

You are retuning an array or rows, you have to loop thru each row

while ($rel = mysql_fetch_assoc($result))
{
    echo'<pre>'.print_r($rel).'<pre/>'; 
    echo"<br/>".$rel['username']."<br/>";
    foreach($rel as $Query[]){ //don't think you need this foreach loop
    echo $Query['username'];
    }
}

also Please, don't use mysql_* functions in new code

Community
  • 1
  • 1
srakl
  • 2,565
  • 2
  • 21
  • 32
1

$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.

http://php.net/manual/en/mysqlinfo.api.choosing.php

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.

Starx
  • 77,474
  • 47
  • 185
  • 261
0

mysql_fetch_assoc returns the row at the current pointer. You need to use it as part of a loop, as the PHP docs show:

while ($row = $result->fetch_assoc()) {
Mitya
  • 33,629
  • 9
  • 60
  • 107
0
while ($row = mysql_fetch_assoc($result)) {
echo $row['username'];
}
Ravi
  • 30,829
  • 42
  • 119
  • 173