开发者

show data from database in table format inside form

I have an input form connected 开发者_Go百科to a database. After [the form is submitted], I want to make a form to show all the data which has been input to the database. I want to show this data in table sortable by name or date.

Please help me.


The high-level steps you want to take are:

  1. Print HTML table header
  2. Establish a connection to the database
  3. Issue a query, and capture the result (e.g. as an array)
  4. Loop through the array, printing each HTML table row
  5. Clean up database objects that may be holding onto memory or db connections
  6. Print HTML table close

The following example is a slightly modified version of example #2 from this page on php.net. I suggest you spend a lot of time on that site - the manual is excellent, and almost every page has numerous working examples in the comments section.

<table>
<?php
// Establish the database connection
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

// Issue the query
$result = mysql_query("SELECT id, name FROM mytable");

// Capture the result in an array, and loop through the array
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    // Print each row as HTML: <tr><td>row 0</td><td>row 1</td>
    printf("<tr><td>%s</td><td>%s</td></tr>", $row[0], $row[1]);  
}
// Free the result set    
mysql_free_result($result);
?>
</table>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜