Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*** mysql hostname ***/
- $hostname = 'localhost';
- /*** mysql username ***/
- $username = 'username';
- /*** mysql password ***/
- $password = 'password';
- try {
- $dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
- /*** echo a message saying we have connected ***/
- echo 'Connected to database<br />';
- /*** The SQL SELECT statement ***/
- $sql = "SELECT * FROM animals";
- /*** fetch into an PDOStatement object ***/
- $stmt = $dbh->query($sql);
- /*** echo number of columns ***/
- $obj = $stmt->fetch(PDO::FETCH_OBJ);
- /*** loop over the object directly ***/
- echo $obj->animal_id.'<br />';
- echo $obj->animal_type.'<br />';
- echo $obj->animal_name;
- /*** close the database connection ***/
- $dbh = null;
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- ?>
Add Comment
Please, Sign In to add comment