Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- class Database
- {
- private $pdo='';
- public $dbConn='';
- private $dbName='';
- private $dbHost='';
- private $dbUser='';
- private $dbPass='';
- public function __construct($database)
- {
- $this->dbName=$database;
- }
- private function dbConnect()
- {
- //place connection in try catch, so errors can be displayed or sent to log
- try
- {
- $this->pdo= new PDO('mysql:dbname='.$this->dbName.';host=xxx.xxx.xx.xxx','abcdef','vwxyz');
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->dbConn=true;
- return true;
- } catch(PDOException $e){
- return $e;
- }
- } //End dbConnect
- public function connect(){
- if ($this->dbConnect()) {
- if ($this->dbConn) {
- $myPDO=$this->pdo;
- return $myPDO;
- }
- } else { return false; }
- }
- function tableExists()
- {
- $findTable="SHOW TABLES FROM ".$this->dbName." WHERE tables_in_".$this->dbName." LIKE 'prod%' ";
- $query=$this->pdo->query($findTable);
- $tablesFound=$query->fetch();
- return $tablesFound;
- } //END Function tableExists.
- public function select($table,$rows='*', $where=null,$order=null,$limit=null)
- {
- is_array($rows) ? $rows=implode(',', $rows) : $rows=$rows;
- $stmt='SELECT '.$rows.' FROM '.$table;
- if ( !is_null($where) )
- $stmt .=' WHERE '.$where;
- if (! is_null($order) )
- $stmt .=' ORDER BY '.$order.' DESC ';
- if (! is_null($limit) )
- $stmt .=' Limit '.$limit;
- //if table exists proceed with Query
- $query=$this->pdo->query($stmt);
- $data=$query->fetchAll(PDO::FETCH_ASSOC);
- return $data;
- } //End Function Select
- public function insert($table,$values,$rows=null)
- {
- $ins='INSERT INTO '.$table;
- if ($rows != null) {
- $ins .= ' ('.implode(',', $rows).')';
- }
- for ($i=0; $i<count($values);$i++ )
- {
- $bindingVals[]=':val'.$i;
- }
- $bindVals=implode(',', $bindingVals);
- $ins .=' VALUES ('.$bindVals.') ';
- $params=array_combine($bindingVals, $values);
- $insert=$this->pdo->prepare($ins);
- foreach ($params as $key => &$value) {
- $insert->bindParam($key, $value,PDO::PARAM_STR);
- }
- try {
- $insert->execute();
- } catch (Exception $e) {
- $error=$e->getMessage();
- echo $error.'<br><br>' ;
- }
- }//END Function insert
- public function setUpdateVals(array $vals)
- {
- foreach ($vals as $k => $v)
- {
- $values[]=$k.'='.$this->pdo->quote($v);
- }
- $values=implode(',',$values);
- return $values;
- }
- public function update($table,$rows,$where=null)
- {
- $sql='UPDATE '.$table.' SET '.$rows;
- if ($where != null)
- {
- $sql.=' WHERE '.$where;
- }
- $stmt=$this->pdo->prepare($sql);
- if ($stmt->execute()) {
- return true;
- }
- }//END Function update
- public function delete($table,$where=null)
- {
- if ( is_null($where) )
- {
- if (confirmTableDelete())
- {
- $sql="DELETE $table";
- }
- } else{
- $sql='DELETE FROM '.$table.'WHERE'.$where;
- }
- $stmt=$this->pdo->prepare($sql);
- $stmt->bindParam(':Value',$Value,$Data_Options);
- $stmt->execute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement