Guest User

Untitled

a guest
May 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. <?php
  2. define('DB_NAME', 'zip');
  3.  
  4. /** MySQL database username */
  5. define('DB_USER', '*****');
  6.  
  7. /** MySQL database password */
  8. define('DB_PASSWORD', '*****');
  9.  
  10. /** MySQL hostname */
  11. define('DB_HOST', 'localhost');
  12. $db = new db();
  13. $zip = new zip($db);
  14.  
  15. $zip->all();
  16.  
  17. class db {
  18. public $conn;
  19.  
  20. public function __construct () {
  21. try
  22. {
  23. $this->conn = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
  24. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  25. }
  26. catch(PDOException $e)
  27. {
  28. echo 'ERROR: ' . $e->getMessage();
  29. }
  30. }
  31. }
  32. class zip {
  33. public $db;
  34.  
  35. function __construct($db) {
  36. //print_r($db);
  37. $this->db = $db;
  38. }
  39. function all()
  40. {
  41. //FETCHING
  42. try
  43. {
  44. $stmt = $this->db->conn->prepare('SELECT * FROM `cities_extended`');
  45. $stmt->execute();
  46. $result = $stmt->fetchAll();
  47. $count = count($result);
  48. if($count)
  49. {
  50. echo $count . ' rows returned...';
  51. }
  52. else
  53. {
  54. echo "No rows returned...";
  55. }
  56. }
  57. catch(PDOException $e)
  58. {
  59. echo 'ERROR: ' . $e->getMessage();
  60. }
  61. }
  62. }
  63. ?>
Add Comment
Please, Sign In to add comment