0

Possible Duplicate:
MySQL returns only one row

I wonder how to select all rows with a single value. EG all rows where the column username is a certain username. Here is the code I have at this moment:

$result = mysql_query("select * from `order` WHERE username='$user'");
$row = mysql_fetch_array($result); 
echo $row['username'];
echo $row['amount'];

This echos only the values in the first row. Any help would be great.

Community
  • 1
  • 1
user1760791
  • 403
  • 3
  • 12
  • 2
    You need to fetch in a loop. `while ($row = mysql_fetch_array($result)) {echo $row['username']; }` – Michael Berkowski Oct 21 '12 at 21:50
  • Thank you, do you also know how I can seperate each of these values? – user1760791 Oct 21 '12 at 21:51
  • What do you mean separate? Like put a space between? `echo $row['username'] . " ";` or a line break ` . '
    '`
    – Michael Berkowski Oct 21 '12 at 21:52
  • [Another example question...](http://stackoverflow.com/questions/6169518/this-php-code-is-only-picking-up-the-first-row-in-my-database-help) – Michael Berkowski Oct 21 '12 at 21:53
  • in table as ... HERE this GOES INSIDE LOOP: LOOP END
    USERNAMEamount
    =$row['username']?>=$row['amount']?>
    –  Oct 21 '12 at 21:53
  • Well I have to make each value of for example amount (a column in my table) a variable. But now it would be all values in one variable. Sorry I am a beginner. – user1760791 Oct 21 '12 at 21:55
  • @user1760791 You need to build your loop accordingly. If each value belongs in a cell in a column, the loop should include the creation of the surrounding ` ` – Michael Berkowski Oct 21 '12 at 21:58
  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – tereško Oct 21 '12 at 23:18

1 Answers1

1

mysql_fetch_array only pulls one row from $result at a time.

Try:

while($row = mysql_fetch_array($result)
{
    echo $row['username'];
    echo $row['amount'];

}
davepmiller
  • 2,620
  • 3
  • 33
  • 61