0

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

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by reyborn
0

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

Edited by diafol: n/a

0

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'];
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.