fortune.php 984 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. //
  3. // Database Test
  4. //
  5. function fortune() {
  6. // Database connection // TODO: use PDO once implemented
  7. $link = mysql_connect('TFB-database', 'benchmarkdbuser', 'benchmarkdbpass');
  8. mysql_select_db('hello_world', $link);
  9. // Store result in array.
  10. $result = mysql_query('SELECT id, message FROM Fortune', $link);
  11. $arr = array();
  12. while ($row = mysql_fetch_array($result)) {
  13. $arr[$row['id']] = $row['message'];
  14. }
  15. mysql_close($link);
  16. $arr[0] = 'Additional fortune added at request time.';
  17. asort($arr);
  18. return $arr;
  19. }
  20. // Set content type
  21. header("Content-type: text/html; charset=utf-8");
  22. ?>
  23. <!DOCTYPE html>
  24. <html>
  25. <head>
  26. <title>Fortunes</title>
  27. </head>
  28. <body>
  29. <table>
  30. <tr>
  31. <th>id</th>
  32. <th>message</th>
  33. </tr>
  34. <?php
  35. foreach ( fortune() as $id => $fortune ) {
  36. ?>
  37. <tr>
  38. <td><?php echo htmlspecialchars($id, ENT_QUOTES, 'UTF-8'); ?></td>
  39. <td><?php echo htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8'); ?></td>
  40. </tr>
  41. <?php } ?>
  42. </table>
  43. </body>
  44. </html>