Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <?php
  2.  
  3. require_once ($_SERVER['DOCUMENT_ROOT'] . '/Mysong/AutoLoad.php');
  4.  
  5. class Query
  6. {
  7. private $dbh;
  8. private $stmt;
  9.  
  10. public function __construct()
  11. {
  12.  
  13. $connection = new DbConnection;
  14.  
  15. if($this->dbh = $connection->dbh){
  16.  
  17. return;
  18.  
  19. } else {
  20.  
  21. echo " Unable to Connect to DB...";
  22. }
  23.  
  24.  
  25. }
  26.  
  27. public function query($query)
  28. {
  29. if($this->dbh != NULL){
  30.  
  31. $this->stmt = $this->dbh->prepare($query);
  32.  
  33. } else {
  34.  
  35. exit;
  36.  
  37. }
  38.  
  39. }
  40.  
  41. public function bind($param, $value, $type = null)
  42. {
  43. if (is_null($type)) {
  44.  
  45. switch (true) {
  46.  
  47. case is_int($value):
  48.  
  49. $type = PDO::PARAM_INT;
  50. break;
  51.  
  52. case is_bool($value):
  53.  
  54. $type = PDO::PARAM_BOOL;
  55. break;
  56.  
  57. case is_null($value):
  58.  
  59. $type = PDO::PARAM_NULL;
  60. break;
  61.  
  62. default:
  63.  
  64. $type = PDO::PARAM_STR;
  65. }
  66. }
  67. $this->stmt->bindValue($param, $value, $type);
  68. }
  69.  
  70. public function execute()
  71. {
  72.  
  73. return $this->stmt->execute();
  74.  
  75. }
  76.  
  77.  
  78. public function resultSet()
  79. {
  80.  
  81. $this->execute();
  82.  
  83. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  84.  
  85. }
  86.  
  87. public function single()
  88. {
  89.  
  90. $this->execute();
  91.  
  92. return $this->stmt->fetch(PDO::FETCH_ASSOC);
  93.  
  94. }
  95.  
  96. public function asObject()
  97. {
  98.  
  99. $this->execute();
  100.  
  101. return $this->stmt->fetch(PDO::FETCH_OBJ);
  102.  
  103. }
  104.  
  105. public function rowCount()
  106. {
  107.  
  108. return $this->stmt->rowCount();
  109.  
  110. }
  111.  
  112. public function findAll($table)
  113. {
  114.  
  115. $sql = "SELECT * FROM $table";
  116.  
  117. $this->stmt = $this->dbh->prepare($sql);
  118.  
  119. $this->stmt->execute();
  120.  
  121. return $this->stmt->fetchAll();
  122.  
  123. }
  124.  
  125. public function closeDb()
  126. {
  127.  
  128. return $this->dbh = NULL;
  129.  
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement