Guest User

Untitled

a guest
Jun 26th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. <?php
  2. class DB {
  3.  
  4.  
  5. private $connection;
  6. private static $_instance;
  7. private $dbhost = ""; // Ip Address of database if external connection.
  8. private $dbuser = ""; // Username for DB
  9. private $dbpass = ""; // Password for DB
  10. private $dbname = ""; // DB Name
  11. private $query = "";
  12.  
  13.  
  14. /*
  15. Get an instance of the Database
  16. @return Instance
  17. */
  18. public static function getInstance(){
  19. if(!self::$_instance) {
  20. self::$_instance = new self();
  21.  
  22. }
  23. return self::$_instance;
  24. }
  25. // Constructor
  26. private function __construct() {
  27. try{
  28.  
  29. $this->connection = new PDO('mysql:host='.$this->dbhost.';dbname='.$this->dbname, $this->dbuser, $this->dbpass);
  30. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  31.  
  32. // Error handling
  33. }catch(PDOException $e){
  34. die("Failed to connect to DB: ". $e->getMessage());
  35. }
  36. }
  37. // Magic method clone is empty to prevent duplication of connection
  38. private function __clone(){}
  39.  
  40. // Get the connection
  41. public function getConnection(){
  42. return $this->connection;
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. //[C] REATE
  52. public function insert($table, $params = []){ //insert $db->insert('table',['key'=>'value']);
  53. $con = self::getConnection();
  54.  
  55. if($this->tableExists($table)){
  56.  
  57. try
  58. {
  59. $sql = 'INSERT INTO `'.$table.'` (`' . implode('`, `',array_keys($params)) . '`) VALUES (' . $this->placeholders($params) . ')';
  60. $stmt = $con->prepare($sql);
  61. try{
  62. $con->beginTransaction();
  63. $stmt->execute(array_values($params));
  64. $con->commit();
  65. }
  66. catch( PDOException $e)
  67. {
  68. $con->rollback();
  69. return $e->getMessage() . "</br>";
  70. }
  71. }
  72. catch( PDOException $e){
  73. return $e->getMessage() . "</br>";
  74. }
  75.  
  76. return true;
  77. }
  78. else
  79. {
  80. return false;
  81. }
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. //[R] ETRIEVE
  90. public function select()
  91. {
  92.  
  93.  
  94.  
  95.  
  96. }
  97.  
  98.  
  99.  
  100. //[U] PDATE
  101. public function update($table, $params = [], $where = null){ //$db->update('table',[],where);
  102. $con = self::getConnection();
  103.  
  104. if($this->tableExists($table)){
  105. try
  106. {
  107. $a = array();
  108.  
  109. foreach($params as $f=>$v){
  110. // Seperate each column out with it's corresponding value
  111. $a[] = $f.'='.$this->placeholders($v);
  112. }
  113.  
  114. $sql = 'UPDATE '.$table.' SET '.implode(',',$a).' WHERE '.$where;
  115. $stmt = $con->prepare($sql);
  116. try{
  117. $con->beginTransaction();
  118. $stmt->execute(array_values($params));
  119. $con->commit();
  120. }
  121. catch( PDOException $e)
  122. {
  123. $con->rollback();
  124. return $e->getMessage() . "</br>";
  125. }
  126. }
  127. catch(PDOException $e){
  128. return $e->getMessage() ."</br>";
  129. }
  130.  
  131. return true;
  132. }
  133. else{
  134. return false;
  135. }
  136.  
  137. }
  138.  
  139.  
  140. //[D] ELETE
  141. public function delete($table, $where = null){
  142. $del='';
  143. $con = self::getConnection();
  144.  
  145. if($this->tableExists($table)){
  146. if($where == null)
  147. {
  148. //$delete = 'DROP TABLE '.$table;
  149. }
  150. else{
  151. $del = 'DELETE FROM '.$table.' WHERE '.$where;
  152. }
  153. try
  154. {
  155. $stmt = $con->prepare($del);
  156. try{
  157. $con->beginTransaction();
  158. $stmt->execute();
  159. $con->commit();
  160. }
  161. catch( PDOException $e)
  162. {
  163. $con->rollback();
  164. return $e->getMessage() . "</br>";
  165. }
  166. }
  167. catch(PDOException $e)
  168. {
  169. return $e->getMessage()."</br>";
  170. }
  171. }
  172. else{
  173. return false;
  174. }
  175.  
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. private function tableExists($tbl) //find table if exists
  184. {
  185.  
  186. try{
  187.  
  188. $tablesInDb = self::getConnection()->query('SHOW TABLES FROM '.$this->dbname.' LIKE "'.$tbl.'"');
  189. if($tablesInDb)
  190. {
  191. if($tablesInDb->fetch() > 0){
  192. return true;
  193. }
  194. else{
  195. return false;
  196. }
  197. }
  198.  
  199. }
  200. catch(PDOException $e){
  201. return 'error: '.$e->getMessage();
  202. }
  203.  
  204.  
  205. }
  206.  
  207.  
  208. /*
  209. * extra functions
  210. */
  211.  
  212.  
  213. //placeholders for binding params
  214. public function placeholders($a = []){
  215.  
  216. $bind = [];
  217.  
  218. for($i = 0; $i < count($a); $i++){
  219. $bind[$i] = '?';
  220. }
  221.  
  222. return implode(',',$bind);
  223.  
  224. }
  225.  
  226. //escapes strings and date values
  227. public function escapeString($val)
  228. {
  229.  
  230. $new_val = self::getConnection()->quote($val);
  231. return $new_val;
  232.  
  233.  
  234. }
  235.  
  236.  
  237.  
  238.  
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245. ?>
Add Comment
Please, Sign In to add comment