reenadak

display print mysql table with php

Sep 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. <html><head><title>MySQL Table Viewer</title></head><body>
  2. <?php
  3. $db_host = 'localhost';
  4. $db_user = 'root';
  5. $db_pwd = '';
  6.  
  7. $database = 'dakdata';
  8. $table = 'contacts';
  9.  
  10. if (!mysql_connect($db_host, $db_user, $db_pwd))
  11.     die("Can't connect to database");
  12.  
  13. if (!mysql_select_db($database))
  14.     die("Can't select database");
  15.  
  16. // sending query
  17. $result = mysql_query("SELECT * FROM {$table}");
  18. if (!$result) {
  19.     die("Query to show fields from table failed");
  20. }
  21.  
  22. $fields_num = mysql_num_fields($result);
  23.  
  24. echo "<h1>Table: {$table}</h1>";
  25. echo "<table border='1' cellspacing='0' cellpadding='3'><tr>";
  26. // printing table headers
  27. for($i=0; $i<$fields_num; $i++)
  28. {
  29.     $field = mysql_fetch_field($result);
  30.     echo "<td>{$field->name}</td>";
  31. }
  32. echo "</tr>\n";
  33. // printing table rows
  34. while($row = mysql_fetch_row($result))
  35. {
  36.     echo "<tr>";
  37.  
  38.     // $row is array... foreach( .. ) puts every element
  39.     // of $row to $cell variable
  40.     foreach($row as $cell)
  41.         if($cell) echo "<td>$cell</td>"; else echo "<td> </td>";
  42.     echo "</tr>\n";
  43. }
  44. mysql_free_result($result);
  45. ?>
  46. </body></html>
Add Comment
Please, Sign In to add comment