Guest User

Untitled

a guest
Aug 14th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2. /*** mysql hostname ***/
  3. $hostname = 'localhost';
  4.  
  5. /*** mysql username ***/
  6. $username = 'username';
  7.  
  8. /*** mysql password ***/
  9. $password = 'password';
  10.  
  11. try {
  12.     $dbh = new PDO("mysql:host=$hostname;dbname=animals", $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.     $obj = $stmt->fetch(PDO::FETCH_OBJ);
  24.  
  25.     /*** loop over the object directly ***/
  26.     echo $obj->animal_id.'<br />';
  27.     echo $obj->animal_type.'<br />';
  28.     echo $obj->animal_name;
  29.  
  30.     /*** close the database connection ***/
  31.     $dbh = null;
  32. }
  33. catch(PDOException $e)
  34.     {
  35.     echo $e->getMessage();
  36.     }
  37. ?>
Add Comment
Please, Sign In to add comment