db = \Zend_Registry::get('db'); }else{ $this->db = $db; } } function insert($table,$data,$return=false) { $fields = array(); $datas = array(); foreach($data as $k=>$v) { $fields[] = '"'.$k.'"'; if(is_int($v) || is_float($v) || is_bool($v)) { $datas[] = $v; }else{ if(preg_match("/\'/",$v)) { $v = preg_replace("/\'/","''",$v); } $datas[] = "'".$v."'"; } } $fields = join(",",$fields); $datas = join(",",$datas); if($return == false){ $sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas)"; if($this->debug == 1) { view::Dump($sql,false); } try{ return $this->db->exec($sql); }catch (Exception $e) { if($this->product) { return false; }else{ echo 'Caught exception: '. $e->getMessage(). "\n"; } } }else{ $sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas) RETURNING id"; if($this->debug == 1) { view::Dump($sql,false); } try{ $sth = $this->db->prepare($sql); if($sth->execute()) { $temp = $sth->fetch(\PDO::FETCH_ASSOC); return $temp['id']; }else{ return false; } }catch (Exception $e) { if($this->product) { return false; }else{ echo 'Caught exception: '. $e->getMessage(). "\n"; } } } }//insert function update($table,$data,$condition="",$return=false) { $ups = array(); foreach($data as $k=>$v) { if(is_int($v) || is_float($v) || is_bool($v)) { $ups[] = '"'.$k.'"='.$v; }else{ if(preg_match("/\'/",$v)) { $v = preg_replace("/\'/","''",$v); } if(preg_match("/\"/",$v)) { $v = preg_replace("/\"/","''",$v); } $ups[] = '"'.$k.'"=\''.$v."'"; } } $fields = join(",",$ups); if(!empty($condition)) { $wheresql = " WHERE ".$condition; }else{ $wheresql = ""; } if($return == false){ try{ $sql = "UPDATE \"".$table."\" SET $fields $wheresql"; if($this->debug == 1) { view::Dump($sql,false); } if($this->db->exec($sql)) { return true; }else{ return false; } }catch (Exception $e) { if($this->product) { return false; }else{ echo 'Caught exception: '. $e->getMessage(). "\n"; } } }else{ try{ $sql = "UPDATE \"".$table."\" SET $fields $wheresql"; if($this->debug == 1) { view::Dump($sql,false); } return $this->db->exec($sql); }catch (Exception $e) { if($this->product) { return false; }else{ echo "
"; echo $sql."\r\n"; echo 'Caught exception: '. $e->getMessage(). "\r\n"; echo ""; } } } }//update //select public function select($opt,$debug = false) { $field = (isset($opt['field']) && !empty($opt['field'])) ? $opt['field']:"*"; $tbl = (isset($opt['table']) && !empty($opt['table'])) ? $opt['table'] : ""; $join = (isset($opt['join']) && !empty($opt['join'])) ? $opt['join'] : ""; $wheresql = (isset($opt['where']) && !empty($opt['where'])) ? " WHERE ".$opt['where']:" "; $limit = (isset($opt['limit']) && !empty($opt['limit'])) ? " LIMIT ".$opt['limit']:""; $offset = (isset($opt['start']) && !empty($opt['start'])) ? " OFFSET ".$opt['start']:""; $order = (isset($opt['order']) && !empty($opt['order'])) ? " ORDER BY ".$opt['order']:""; $order .= (isset($opt['sort']) && !empty($opt['sort']) && $order !== '') ? " ".$opt['sort']:""; $sql = "SELECT $field FROM $tbl " . $join . " " . $wheresql . " " . $order . " " . $limit . " " . $offset . " "; if($debug) return $sql; $rs = $this->db->query($sql); return $rs->fetchAll(); } public function chk($p) { if(!isset($p) || empty($p)) { return false; }else{ return true; } } }