Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. <?php
  2. // DATABASE CONNECTION
  3.  
  4.  
  5. // define login credentials for the database
  6. // these must match the settings on your database
  7. $username = "root";
  8. $password = "";
  9. $host = "localhost";
  10. $database = "ao16_dummy_database";
  11. $table = "averages_final";
  12.  
  13.  
  14. // connect to MySQL server
  15. // the function mysqli_connect(...) returns a link to the database which we store in the variable $link
  16. $link = mysqli_connect($host, $username, $password) or die("Connection to database server failed!");
  17.  
  18.  
  19. // NB: using die() is not the most elegant way to handle errors because it causes the site to only return
  20. // the error message, no styling and so on. A more elegant way would be to ensure the site keeps running
  21. // whenever possible and else print out errors in a more appealing page design ;)
  22.  
  23.  
  24. // select the database to work with
  25. mysqli_select_db($link, $database);
  26.  
  27. // DATABASE ACCESS (read)
  28.  
  29. // fetch all users from the database
  30. $result = mysqli_query($link, "SELECT * FROM $table");
  31. // SELECT * FROM $table is SQL syntax and means 'select everything from the table given and return it'
  32.  
  33. echo "<table>\n";
  34. // go through each entry
  35. while ($data = mysqli_fetch_array($result))
  36. {
  37. // print the details
  38. echo "\t<tr>\n";
  39. echo "\t\t<td>" . $data['name'] . "</td>\n\t\t<td>" . $data['final'] . "</td>\n";
  40. echo "\t</tr>\n";
  41. // the echo keyword is used to output data to the browser, usually HTML
  42. // the while keyword denotes that this code is repeated as long as there is data ($data = mysqli_fetch_array($result))
  43. }
  44. echo "</table>\n";
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement