Guest User

Untitled

a guest
Aug 27th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. <?php
  2. /*** mysql hostname ***/
  3. $hostname = 'localhost';
  4.  
  5. /*** mysql username ***/
  6. $username = 'root';
  7.  
  8. /*** mysql password ***/
  9. $password = 'root';
  10.  
  11. try {
  12. $dbh = new PDO("mysql:host=$hostname;dbname=pdo", $username, $password);
  13. /*** echo a message saying we have connected ***/
  14. echo 'Connected to database<br />';
  15.  
  16. /*** The SQL SELECT statement ***/
  17. $sql = "SELECT * FROM animals";
  18.  
  19. /*** fetch into an PDOStatement object ***/
  20. $stmt = $dbh->query($sql);
  21.  
  22. /*** echo number of columns ***/
  23. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  24.  
  25. /*** loop over the object directly ***/
  26. foreach($result as $key=>$val)
  27. {
  28. echo $key.' - '.$val.'<br />';
  29. }
  30.  
  31. /*** close the database connection ***/
  32. $dbh = null;
  33. }
  34. catch(PDOException $e)
  35. {
  36. echo $e->getMessage();
  37. }
  38. ?>
Add Comment
Please, Sign In to add comment