简体   繁体   中英

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? 

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'];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM