Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: artem
  5. * Date: 7/23/13
  6. * Time: 12:58 PM
  7. * To change this template use File | Settings | File Templates.
  8. */
  9.  
  10. class Database {
  11. private $host = DB_HOST;
  12. private $user = DB_USER;
  13. private $pass = DB_PASS;
  14. private $dbname = DB_NAME;
  15.  
  16. private $stmt;
  17.  
  18. /**
  19. * construction
  20. */
  21. public function __construct(){
  22. // Set DSN
  23. $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  24. // Set options
  25. $options = array(
  26. PDO::ATTR_PERSISTENT => true,
  27. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  28. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  29. );
  30. // Create a new PDO instanace
  31. try{
  32. $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  33. }
  34. // Catch any errors
  35. catch(PDOException $e){
  36. $this->error = $e->getMessage();
  37. }
  38. }
  39.  
  40. /**
  41. * prepare SQL query
  42. * @param $query
  43. */
  44. public function query($query){
  45. $this->stmt = $this->dbh->prepare($query);
  46. }
  47.  
  48. /**
  49. * Bind parameters
  50. * @param $param
  51. * @param $value
  52. * @param null $type
  53. */
  54. public function bind($param, $value, $type = null){
  55. if (is_null($type)) {
  56. switch (true) {
  57. case is_int($value):
  58. $type = PDO::PARAM_INT;
  59. break;
  60. case is_bool($value):
  61. $type = PDO::PARAM_BOOL;
  62. break;
  63. case is_null($value):
  64. $type = PDO::PARAM_NULL;
  65. break;
  66. default:
  67. $type = PDO::PARAM_STR;
  68. }
  69. }
  70. $this->stmt->bindValue($param, $value, $type);
  71. }
  72.  
  73. /**
  74. * execute query
  75. * @return mixed
  76. */
  77. public function execute(){
  78. return $this->stmt->execute();
  79. }
  80.  
  81. /**
  82. * result set
  83. * @param int $method
  84. * @return mixed
  85. */
  86. public function resultset($method=PDO::FETCH_ASSOC){
  87. $this->execute();
  88. return $this->stmt->fetchAll($method);
  89. }
  90.  
  91. /**
  92. * get single result
  93. * @return mixed
  94. */
  95. public function single(){
  96. $this->execute();
  97. return $this->stmt->fetch(PDO::FETCH_ASSOC);
  98. }
  99.  
  100. /**
  101. * num of rows
  102. * @return mixed
  103. */
  104. public function rowCount(){
  105. return $this->stmt->rowCount();
  106. }
  107.  
  108. /**
  109. * last inserted item ID
  110. * @return string
  111. */
  112. public function lastInsertId(){
  113. return $this->dbh->lastInsertId();
  114. }
  115.  
  116. /**
  117. * get fetched IDs
  118. *
  119. * @return array|bool
  120. */
  121. public function fetched_ids() {
  122. $this->execute();
  123. $res = array();
  124. $arr_fetched = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  125. if( !$arr_fetched ) return false;
  126.  
  127. foreach($arr_fetched as $row) {
  128. $res[] = current($row);
  129. }
  130. return $res;
  131. }
  132.  
  133. /**
  134. *
  135. * @return bool
  136. */
  137. public function beginTransaction(){
  138. return $this->dbh->beginTransaction();
  139. }
  140.  
  141. /**
  142. * @return bool
  143. */
  144. public function endTransaction(){
  145. return $this->dbh->commit();
  146. }
  147.  
  148. /**
  149. * @return bool
  150. */
  151. public function cancelTransaction(){
  152. return $this->dbh->rollBack();
  153. }
  154.  
  155. /**
  156. * @return mixed
  157. */
  158. public function debugDumpParams(){
  159. return $this->stmt->debugDumpParams();
  160. }
  161.  
  162. /**
  163. * MySQL add function
  164. * @param $table
  165. * @param $values
  166. * @return string
  167. */
  168. public function add($table, $values){
  169. $fieldsArr = array_keys($values);
  170. $fields = implode(", ", $fieldsArr);
  171. $bindFields = implode(", :", $fieldsArr);
  172.  
  173. $this->query('INSERT INTO ' . $table . ' (' . $fields . ') VALUES (:' . $bindFields . ')');
  174.  
  175. foreach($values as $field => $value){
  176. $this->bind(":".$field, $value);
  177. }
  178.  
  179. try{
  180. $this->execute();
  181. } catch(PDOException $e) {
  182. return $e->getMessage();
  183. }
  184.  
  185. return (int) $this->lastInsertId();
  186. }
  187.  
  188. /**
  189. * MySQL add multiple records function
  190. * @param $table
  191. * @param $values
  192. * @param $keyArr - foreign key for programs
  193. * @return bool
  194. */
  195. public function addMulti($table, $values, $keyArr=array()){
  196. foreach($values as $arrValues){
  197. $arrValues = array_merge($keyArr, $arrValues);
  198. $fieldsArr = array_keys($arrValues);
  199. $insertFields = implode(", ", $fieldsArr);
  200. $bindFields = implode(", :", $fieldsArr);
  201.  
  202. $newFieldsArr = array();
  203.  
  204. foreach($fieldsArr as $fieldName){
  205. array_push($newFieldsArr, $fieldName." = :".$fieldName);
  206. }
  207.  
  208. $updateFields = implode(", ", $newFieldsArr);
  209.  
  210. $this->query('INSERT INTO ' . $table . ' (' . $insertFields . ') VALUES (:' . $bindFields . ')
  211. ON DUPLICATE KEY UPDATE '. $updateFields);
  212.  
  213. foreach($arrValues as $field => $value){
  214. $this->bind(":".$field, $value);
  215. }
  216.  
  217. $this->execute();
  218. }
  219.  
  220. // yeah at the end it will always be true
  221. return true;
  222. }
  223.  
  224. /**
  225. * MySQL update function
  226. * @param $table
  227. * @param $values
  228. * @param $id
  229. * @return mixed
  230. */
  231. public function update($table, $values, $id){
  232. $fieldsArr = array_keys($values);
  233. $newFieldsArr = array();
  234.  
  235. foreach($fieldsArr as $fieldName){
  236. array_push($newFieldsArr, $fieldName." = :".$fieldName);
  237. }
  238.  
  239. $fields = implode(", ", $newFieldsArr);
  240.  
  241. $this->query('UPDATE ' . $table . ' SET ' . $fields . ' WHERE ID=:id');
  242. $this->bind(":id", $id);
  243.  
  244. foreach($values as $field => $value){
  245. $this->bind(":".$field, $value);
  246. }
  247.  
  248. return $this->execute();
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement