Advertisement
Guest User

Untitled

a guest
Aug 21st, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. <?PHP
  2.  
  3. class Database
  4. {
  5.  
  6. private $pdo='';
  7. public $dbConn='';
  8.  
  9. private $dbName='';
  10. private $dbHost='';
  11. private $dbUser='';
  12. private $dbPass='';
  13.  
  14.  
  15. public function __construct($database)
  16. {
  17. $this->dbName=$database;
  18. }
  19.  
  20. private function dbConnect()
  21. {
  22. //place connection in try catch, so errors can be displayed or sent to log
  23. try
  24. {
  25. $this->pdo= new PDO('mysql:dbname='.$this->dbName.';host=xxx.xxx.xx.xxx','abcdef','vwxyz');
  26. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  27.  
  28. $this->dbConn=true;
  29.  
  30. return true;
  31.  
  32. } catch(PDOException $e){
  33.  
  34. return $e;
  35.  
  36. }
  37.  
  38. } //End dbConnect
  39.  
  40.  
  41. public function connect(){
  42.  
  43. if ($this->dbConnect()) {
  44.  
  45. if ($this->dbConn) {
  46.  
  47. $myPDO=$this->pdo;
  48.  
  49. return $myPDO;
  50. }
  51.  
  52.  
  53. } else { return false; }
  54.  
  55. }
  56.  
  57.  
  58. function tableExists()
  59. {
  60.  
  61. $findTable="SHOW TABLES FROM ".$this->dbName." WHERE tables_in_".$this->dbName." LIKE 'prod%' ";
  62.  
  63. $query=$this->pdo->query($findTable);
  64. $tablesFound=$query->fetch();
  65.  
  66. return $tablesFound;
  67.  
  68. } //END Function tableExists.
  69.  
  70.  
  71. public function select($table,$rows='*', $where=null,$order=null,$limit=null)
  72. {
  73.  
  74. is_array($rows) ? $rows=implode(',', $rows) : $rows=$rows;
  75.  
  76. $stmt='SELECT '.$rows.' FROM '.$table;
  77.  
  78. if ( !is_null($where) )
  79. $stmt .=' WHERE '.$where;
  80.  
  81. if (! is_null($order) )
  82. $stmt .=' ORDER BY '.$order.' DESC ';
  83.  
  84. if (! is_null($limit) )
  85. $stmt .=' Limit '.$limit;
  86.  
  87. //if table exists proceed with Query
  88. $query=$this->pdo->query($stmt);
  89.  
  90. $data=$query->fetchAll(PDO::FETCH_ASSOC);
  91.  
  92. return $data;
  93.  
  94. } //End Function Select
  95.  
  96.  
  97. public function insert($table,$values,$rows=null)
  98. {
  99. $ins='INSERT INTO '.$table;
  100.  
  101. if ($rows != null) {
  102.  
  103. $ins .= ' ('.implode(',', $rows).')';
  104. }
  105.  
  106. for ($i=0; $i<count($values);$i++ )
  107. {
  108. $bindingVals[]=':val'.$i;
  109. }
  110.  
  111. $bindVals=implode(',', $bindingVals);
  112. $ins .=' VALUES ('.$bindVals.') ';
  113.  
  114. $params=array_combine($bindingVals, $values);
  115. $insert=$this->pdo->prepare($ins);
  116.  
  117. foreach ($params as $key => &$value) {
  118. $insert->bindParam($key, $value,PDO::PARAM_STR);
  119. }
  120.  
  121.  
  122. try {
  123. $insert->execute();
  124. } catch (Exception $e) {
  125. $error=$e->getMessage();
  126.  
  127. echo $error.'<br><br>' ;
  128. }
  129.  
  130. }//END Function insert
  131.  
  132.  
  133.  
  134. public function setUpdateVals(array $vals)
  135. {
  136. foreach ($vals as $k => $v)
  137.  
  138. {
  139. $values[]=$k.'='.$this->pdo->quote($v);
  140. }
  141.  
  142. $values=implode(',',$values);
  143.  
  144. return $values;
  145. }
  146.  
  147.  
  148. public function update($table,$rows,$where=null)
  149. {
  150.  
  151. $sql='UPDATE '.$table.' SET '.$rows;
  152. if ($where != null)
  153. {
  154. $sql.=' WHERE '.$where;
  155. }
  156.  
  157. $stmt=$this->pdo->prepare($sql);
  158. if ($stmt->execute()) {
  159.  
  160. return true;
  161. }
  162.  
  163. }//END Function update
  164.  
  165.  
  166. public function delete($table,$where=null)
  167. {
  168.  
  169. if ( is_null($where) )
  170. {
  171.  
  172. if (confirmTableDelete())
  173. {
  174. $sql="DELETE $table";
  175. }
  176.  
  177. } else{
  178.  
  179. $sql='DELETE FROM '.$table.'WHERE'.$where;
  180. }
  181.  
  182. $stmt=$this->pdo->prepare($sql);
  183. $stmt->bindParam(':Value',$Value,$Data_Options);
  184. $stmt->execute();
  185.  
  186. }
  187.  
  188.  
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement