[Solved]Query DB:Display on Table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

[Solved]Query DB:Display on Table

Post by scts102 »

Hey.

Sorry to bother you guys with another "duh" question, but here it goes:

How can I get all of the data from a specific table, and display it on a table using php? I can connect to the db, and run a Select * from table query, but I have no idea how to parse it and display it in a table....

For example, the table might have the following fields: Name (Index), email, phone. Now the db has all of this info. I need the php script to get all of the data, and for each "user", run a

Code: Select all

<tr><td>$name</td> (etc) </tr>
I hope I was clear.

Thanks for the help,
Larry
Last edited by scts102 on Sun Sep 04, 2005 4:22 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

function filt($value)
{
  return ($value === null ? '<i>NULL</i>' : $value);
}

$query = mysql_query('SELECT * FROM table') or die(mysql_error());
$first = true;
$out = .'<table>';
while($row = mysql_fetch_row($query))
{
  if($first)
  {
    $out .= '<tr>';
    for($i = 0, $j = count($row); $i < $j; $i++)
    {
      $out .= '<th>';
      $temp = mysql_fetch_field($query,$i);
      $out .= $temp['name'];
      $out .= '</th>';
    }
    $out .= '</tr>'."\n";
    $first = false;
  }
  $out .= '<tr><td>'.implode('</td><td>',array_map('filt',$row)).'</td></tr>'."\n";
}
$out .= '</table>';
Post Reply