回声表从phpMyAdmin到html表

回声表从phpMyAdmin到html表

问题描述:

How would you echo out a MySQL table into a html table. Like phpMyAdmin shows a table, just a basic table structure with the name of the fields and all the records in that table.

Any help would be appreciated

如何将MySQL表回显到html表中。 就像phpMyAdmin显示一个表,只是一个基本的表结构,其中包含字段的名称和该表中的所有记录。 p>

任何帮助将不胜感激 p> div >

This code show the a dinamic table with all the field and all the rows returned from a query...

This works for all pourpose you need

  • list all tables
  • show table field description
  • show all table rows

    <?php
     // list all the table in database
     $result = mysql_query("show tables");
     // show all the field with type, lenght,description of a specific table
     $result = mysql_query("DESCRIBE table_name");
     // show a specific tables row 
     $result = mysql_query("SELECT * FROM table_name");
     if (mysql_num_rows($result)>0){
     $r = mysql_fetch_array($result,MYSQL_ASSOC);
     $table="<table><tr>";
     $firstLine="<tr>";
     foreach ($r as $k => $v){
       $table .="<td>".$k."</td>";
       $firstLine .="<td>".$v."</td>";
     }
     $table.="</tr>".$firstLine."</tr>";
     while($r = mysql_fetch_array($result,MYSQL_ASSOC)){
       $table.="<tr>";
       foreach($r as $k => $v)
         $table.="<td>".$v."</td>";
       $table.="</tr>";
     }
      $table .="</table>";
     echo $table;
    }
    ?>
    

The SQL you're looking for is SHOW TABLES to show all tables in your current database and DESCRIBE tablename to show all columns and their information. You can execute that query and loop through the results to print your table layout.

If that's not what you're looking for, do you mean to (auto-)generate a forms for CRUD (Create, Read, Update, Delete) operations, based on your table layout? Then you could look at something like php scaffold or Doctrine.

<table>
<th>NAME</th>
<th>ID</th>
<?php
$query = "SELECT name, id FROM table";
while($data = mysql_fetch_assoc($query)){
$name = $data['name'];
$id = $data['id'];
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$id.'</td>'; 
echo '</tr>';
}
?>
</table>
  1. Create query that SELECT the data you want to retrieve
  2. Enclose it in mysql_fetch_assoc() plus a while loop if your getting more than 1 row from your query.

I think this link will help you out.

http://in3.php.net/mysql_field_name