I've searched & searched and haven't figured it out, any help is great!

I need to display certain fields from my table, right now it only displays the first row of the table. I need to display all rows in the table.

Code I have right now

$query = "SELECT name, desc, city FROM table";

$result = mysql_query($query) or die('Could not find info');
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');

{
$name = $row['name'];
$desc = $row['desc'];
$city = $row['city'];
}
echo $name . "\n" 
blah blah blah

How do you display all info from all rows in database table? I have an id for each row but id isn't used on this page. Thanks -Giggaman

Member Avatar for diafol

You need to use a while loop:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $name = $row['name'];
  $desc = $row['desc'];
  $city = $row['city'];
  echo $name . "\n";
  blah blah blah...
}

You can make a fucntion and assign your result set into an array for further used.

function getResult()

$query = "SELECT name, desc, city FROM table";
$result = mysql_query($query) or die('Could not find info');

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $result['name'] = $row['name'];
  $result['desc'] = $row['desc'];
  $result['city'] = $row['city'];
}

return $result;
//To retrieve the result array;
$res = getResult();
//loop through an array
foreach ($res as $r) {
echo  $r['name'] .' '.$r['desc'].' '.$r['city'];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.