Advertisement
Guest User

Untitled

a guest
Jul 31st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. <?php
  2.  
  3. class Db
  4. {
  5. /**
  6. * @var PDO
  7. */
  8. private $db;
  9.  
  10. /**
  11. * Db constructor.
  12. * @param string $dbName
  13. * @param null|string $dbHost
  14. * @param null|string $dbUser
  15. * @param null|string $dbPass
  16. */
  17. function __construct($dbName = null, $dbHost = null, $dbUser = null, $dbPass = null) {
  18. $dsn = "mysql:dbname={$dbName};host={$dbHost}";
  19. try {
  20. $this->db = new PDO($dsn, $dbUser, $dbPass, array(
  21. PDO::ATTR_PERSISTENT => false,
  22. PDO::ATTR_EMULATE_PREPARES => true,
  23. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  24. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  25. PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '+00:00',NAMES utf8;",
  26. )
  27. );
  28. } catch(Exception $e) {
  29. var_dump("PDO Error: " . $e->getMessage());
  30. }
  31. }
  32.  
  33. /**
  34. * @param $query
  35. * @param array $parameters
  36. * @return array|bool
  37. * @throws Exception
  38. */
  39. public function query($query, $parameters = array()) {
  40. // Sanity check
  41. if(strpos($query, ";") !== false) {
  42. throw new Exception("Semicolons are not allowed in queries. Use parameters instead.");
  43. }
  44.  
  45. try {
  46. // Prepare the query
  47. $stmt = $this->db->prepare($query);
  48.  
  49. // Execute with parameters
  50. $stmt->execute($parameters);
  51.  
  52. // Check for errors
  53. if($stmt->errorCode() != 0) {
  54. return false;
  55. }
  56.  
  57. // Fetch the results into an associative array
  58. $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  59.  
  60. // Close the cursor
  61. $stmt->closeCursor();
  62.  
  63. // Return the data
  64. return $result;
  65. } catch(Exception $e) {
  66. throw new Exception("PDO Query Error: " . $e->getMessage());
  67. }
  68. }
  69.  
  70. /**
  71. * @param string $query
  72. * @param string $field
  73. * @param array $parameters
  74. * @return null|string
  75. * @throws Exception
  76. */
  77. public function queryField($query, $field, $parameters = array()) {
  78. // Get the result
  79. $result = $this->query($query, $parameters);
  80.  
  81. // Check if it has results
  82. if(count($result) == 0) {
  83. return null;
  84. }
  85.  
  86. // Bind the first result row to $resultRow
  87. $resultRow = $result[0];
  88.  
  89. // Return the result + the field requested
  90. return $resultRow[$field];
  91. }
  92.  
  93. /**
  94. * @param $query
  95. * @param array $parameters
  96. * @return array
  97. * @throws Exception
  98. */
  99. public function queryRow($query, array $parameters = array()) {
  100. // Get the result
  101. $result = $this->query($query, $parameters);
  102.  
  103. // Check for any results
  104. if(count($result) >= 1) {
  105. return $result[0];
  106. }
  107.  
  108. // There are no results
  109. return array();
  110. }
  111.  
  112. /**
  113. * @param string $query
  114. * @param array $parameters
  115. * @param bool $returnID
  116. * @return int
  117. * @throws Exception
  118. */
  119. public function execute($query, $parameters = array(), $returnID = false) {
  120. try {
  121. if(stristr($query, ";")) {
  122. $explodedQuery = explode(";", $query);
  123. foreach($explodedQuery as $newQry) {
  124. $stmt = $this->db->prepare($newQry);
  125. $stmt->execute($parameters);
  126. }
  127. }
  128. else {
  129. // Prepare the query
  130. $stmt = $this->db->prepare($query);
  131.  
  132. // Execute with parameters
  133. $stmt->execute($parameters);
  134. }
  135.  
  136. // Check for errors
  137. if($stmt->errorCode() != 0) {
  138. $this->db->rollBack();
  139. return false;
  140. }
  141.  
  142. // Get the ID of what we just inserted, if it exists
  143. $returnID = $returnID ? $this->db->lastInsertId() : 0;
  144.  
  145. // Row count that was changed
  146. $rowCount = $stmt->rowCount();
  147.  
  148. // Close the cursor
  149. $stmt->closeCursor();
  150.  
  151. if($returnID) {
  152. return $returnID;
  153. }
  154.  
  155. return $rowCount;
  156. } catch(Exception $e) {
  157. throw new Exception("PDO Query Error: " . $e->getMessage());
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement