Posted By

brent-man on 10/13/08


Tagged

mysql html tables dump


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jamesming
vali29


Dump a database to HTML tables


 / Published in: PHP
 

URL: http://www.php.happycodings.com/Database_Related/code9.html

  1. <?php
  2.  
  3. $dbname = "test";
  4. $loginname = "user";
  5. $loginpass = "password";
  6. $dbhost = "localhost";
  7.  
  8. echo('<html><body bgcolor="#FFFFFF">');
  9. echo('<font face="arial" size="+4"><center>');
  10. echo("Database $dbname");
  11.  
  12. $id_link = @mysql_connect($dbhost, $loginname, $loginpass);
  13.  
  14. $tables = mysql_list_tables($dbname, $id_link);
  15.  
  16. $num_tables = mysql_num_rows($tables);
  17.  
  18. // store table names in an array
  19. $arr_tablenames[] = '';
  20.  
  21. // store number of fields per table(index 0,1,2..) in an array
  22. $arr_num_fields[] = '';
  23. for ($i=0; $i < $num_tables; $i++) {
  24. $arr_tablenames[$i] = mysql_tablename($tables, $i);
  25. $arr_num_fields[$i] = mysql_num_fields(mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link));
  26. }
  27.  
  28. // store field names in a multidimensional array:
  29. // [i] == table number, [ii] == field number for that table
  30. for ($i=0; $i < $num_tables; $i++) {
  31. for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
  32. $result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
  33. $hash_field_names[$i][$ii] = mysql_field_name($result, $ii);
  34. }
  35. }
  36.  
  37. for ($i=0; $i < $num_tables; $i++) {
  38. echo("<center><h2>Table $arr_tablenames[$i] </h2></center>");
  39. echo('<table align="center" border="1"><tr>');
  40. $result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
  41. for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
  42. echo("<th>");
  43. echo $hash_field_names[$i][$ii];
  44. echo("</th>");
  45. }
  46. echo("</tr><tr>");
  47. $number_of_rows = @mysql_num_rows($result);
  48. for ($iii = 0; $iii < $number_of_rows; $iii++) {
  49. $record = @mysql_fetch_row($result);
  50. for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
  51. echo("<td>");
  52. echo $record[$ii];
  53. echo("</td>");
  54. }
  55. echo("</tr>");
  56. }
  57. echo("</table>");
  58. }
  59.  
  60.  
  61.  
  62. echo('</body></html>');
  63. ?>

Report this snippet  

You need to login to post a comment.