Mr. Banana
Mr. Banana

Reputation: 11

PHP: Output data from MySQL columns

I wanted to get all the rows in a given column and output them, but my code is just not working:

$result = mysql_query("SELECT Name,Number FROM Users WHERE Customer_ID = 1);
  if (!$result) {
    die("Query to show fields from table failed");
  }
$row = mysql_fetch_assoc($result);

echo $row['Name'];
echo $row['Number'];

^ This code is only displaying the first row, how can I list all the rows? 

Upvotes: 0

Views: 535

Answers (2)

netemp
netemp

Reputation: 4185

In order to display all the rows, you need to use 'while' loop as following:

while($row = mysql_fetch_assoc($result)){

    echo $row['Name'];
    echo $row['Number'];
    echo '<br />';
}

Hope this helps.

Upvotes: 0

Eduard Luca
Eduard Luca

Reputation: 6611

You have to loop through the rows. Getting the mysql_fetch_assoc 1 time, will only return you 1 row (the first one), so you have to do a mysql_fetch_assoc for each row in the result. Something like this would do:

while($row = mysql_fetch_assoc($result))
    echo $row['Name']." ".$row['Number'];

Upvotes: 3

Related Questions