Get DB data using PHP


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. // Establish database connection
  3. $db_host = 'localhost';
  4. $db_name = 'DB NAME';
  5. $db_user = 'YOUR USER NAME';
  6. $db_pass = 'YOUR PASSWORD';
  7. $db_table = 'DB TABLE';
  8.  
  9. // Connect to database
  10. $link = mysql_connect($db_host, $db_user, $db_pass);
  11. $db = mysql_select_db($db_name);
  12.  
  13. // Generate SELECT query
  14. $sql = "SELECT date_saved, atm, access, loan, checking, online, advantage, contact_name, contact_email, contact_zip FROM quiz_responses ORDER BY date_saved DESC";
  15.  
  16. $result = mysql_query($sql, $link);
  17.  
  18. // Loop through the results/rows
  19. if ($result) {
  20.  
  21. echo "<table id='quiz_responses' cellspacing='0' cellpadding='3' border='0'>n"; // Open a table
  22. echo "<thead>n<tr>n";
  23. echo "<th>Date Saved</th>";
  24. echo "<th>Contact Name</th>";
  25. echo "<th>Contact Email</th>";
  26. echo "<th>Contact Zip</th>";
  27.  
  28. echo "<th>ATM Response</th>";
  29. echo "<th>Access Response</th>";
  30. echo "<th>Loan Response</th>";
  31. echo "<th>Checking Response</th>";
  32. echo "<th>Online Response</th>";
  33. echo "<th>Advantage Response</th>";
  34. echo "</tr>n</thead>n<tbody>";
  35.  
  36. while($row = mysql_fetch_array($result)) { // Pull each row from the table
  37. echo "<tr>n";
  38. echo "<td>" . $row['date_saved'] . "</td>";
  39. echo "<td>" . $row['contact_name'] . "</td>";
  40. echo "<td>" . $row['contact_email'] . "</td>";
  41. echo "<td>" . $row['contact_zip'] . "</td>";
  42. echo "<td>" . $row['atm'] . "</td>";
  43.  
  44. echo "<td>" . $row['atm'] . "</td>";
  45. echo "<td>" . $row['access'] . "</td>";
  46. echo "<td>" . $row['loan'] . "</td>";
  47. echo "<td>" . $row['checking'] . "</td>";
  48. echo "<td>" . $row['online'] . "</td>";
  49. echo "<td>" . $row['advantage'] . "</td>";
  50.  
  51. echo "</tr>n";
  52. }
  53.  
  54. echo "</tbody>n</table>"; // Close the table
  55.  
  56.  
  57. } else {
  58.  
  59. echo "No records were found. Perhaps the database is empty?";
  60.  
  61. }
  62.  
  63. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.