Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. <?php
  2. //connection details
  3. $host = 'localhost';
  4. $dbname = 'database name';
  5. $dbusername = 'database username';
  6. $password = 'db password';
  7.  
  8. //make a connection
  9. $db = new PDO("mysql:$host=;port=8889;$dbname=", $dbusername, $password);
  10. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.  
  12.  
  13. //get records from database if there's an error catch it.
  14. try {
  15.  
  16. //select columns to select from the database table
  17. $stmt = $db->query('SELECT id, name, address FROM table');
  18.  
  19. //loop through the rows
  20. while($row = $stmt->fetch()){
  21.  
  22. //print the output
  23. echo '<p>';
  24. echo 'id: '.$row['id'].'<br />';
  25. echo 'name: '.$row['name'].'<br />';
  26. echo 'addres: '.$row['address'];
  27. echo '</p>';
  28.  
  29. }// end the while
  30.  
  31. } catch(PDOException $e) {
  32.  
  33. //display any error messages
  34. echo $e->getMessage();
  35.  
  36. }
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement