Advertisement
Guest User

Untitled

a guest
Feb 28th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. <?php
  2. class Database {
  3. // Properties:
  4. private $host;
  5. private $dbname;
  6. private $user;
  7. private $pass;
  8.  
  9. private $error;
  10. private $stmt;
  11. private $con;
  12.  
  13. // Methods:
  14. public function __construct($host, $dbname, $user, $pass) {
  15. $this->host = $host;
  16. $this->dbname = $dbname;
  17. $this->user = $user;
  18. $this->pass = $pass;
  19.  
  20. try {
  21. $this->con = new PDO("mysql:host=".$this->host.";dbname=".$this->dbname, $this->user, $this->pass);
  22. $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  23. } catch (PDOException $e) {
  24. $this->error = $e->getMessage();
  25. return $this->error;
  26. }
  27. } // end "__construct".
  28.  
  29. # $query is the SQL query to send off to the database.
  30. # $bind is the associative array with all the bound parameters
  31. public function query($query, $bind = null) {
  32. $this->stmt = $this->con->prepare($query);
  33.  
  34. if ($bind) {
  35. $this->stmt->execute($bind);
  36. } else {
  37. $this->stmt->execute();
  38. }
  39.  
  40. $check = explode(" ", $query);
  41.  
  42. if ($check[0] == "SELECT") {
  43. $result = $this->stmt->fetchAll(PDO::FETCH_OBJ);
  44.  
  45. if (isset($result[0])) {
  46. return $result;
  47. } else {
  48. return false;
  49. }
  50. } else {
  51. return true;
  52. }
  53. } // end method "query".
  54. } // end class.
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement