Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. <?php
  2.  
  3. function selectAllWithFetch($result){
  4. // create the table
  5. print("<table border='4'>");
  6.  
  7. // add the rows to the table
  8. while($row = $result->fetch(PDO::FETCH_ASSOC)) {
  9. //add a row
  10. print ("<tr>");
  11.  
  12. //print a cell
  13. print ("<td>$row[date]</td> ");
  14. print ("<td> $row[time] </td>");
  15. print ("<td>$row[posted_by] </td>");
  16. print ("<td> $row[result_information] </td>");
  17. // print ("<td> $row[username] </td>");
  18. //close row
  19. print ("</tr>");
  20.  
  21. }
  22.  
  23. //close table
  24. print ("</table");
  25.  
  26. function selectWithFetchAll($result) {
  27. print("<pre>");
  28. print_r($result->fetchAll(PDO::FETCH_obj));
  29. print("</pre>");
  30.  
  31.  
  32. }
  33.  
  34. /* Main body */
  35.  
  36. // print_r(PDO::getAvailableDrivers());
  37.  
  38. //connect to the DB
  39. $dsn = 'mysql:host=localhost;dbname=surgery';
  40. $user = 'root';
  41. $password = '';
  42.  
  43. try {
  44. $db = new PDO($dsn, $user, $password);
  45. } catch (PDOException $e) {
  46. die('Sorry, database problem');
  47. }
  48.  
  49. $default = "Lina_Carter"; // set this to some username that has tests.
  50. $username = $_SESSION['username'] ?? $default;
  51. $queryStr = "SELECT * FROM test_results WHERE username = :username";
  52. $query = $db->prepare($queryStr);
  53. $result = $query->execute([':username' => $username]);
  54.  
  55. selectWithFetchAll($result);
  56. }
  57. ?>
  58. <a href="myaccount.php"><p>Back to my account</p></a>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement