dbname=DB_NAME; else $this->dbname=$dbname; if (!$dbuser) $this->dbuser=DB_USER; else $this->dbuser=$dbuser; if (!$dbpass) $this->dbpw=DB_PASS; else $this->dbpw=$dbpass; if (!$dbserver) $this->dbserver=DB_SERVER; else $this->dbserver=$dbserver; $this->db=new mysqli($this->dbserver,$this->dbuser,$this->dbpw,$this->dbname); if (!$this->db) { echo "Failed to connect to MySQL: (" . $this->db->connect_errno . ") " . $this->db->connect_error; exit; } $this->sqlQuery("SET NAMES 'utf8'"); } private function getAsArray($data) { $el=0; reset($data); foreach ($data as $key => $val) { //while (list($key,$val)=each($data)) { if (($key)&&(!(is_numeric($key)))) { $this->keys[$el]=$key; $this->values[$el]=$val; $el++; } } return $el; } function inserted_id(){ return mysql_insert_id($this->db); } function sqlQuery($q) { $this->res=$this->db->query($q); if (!$this->res) { echo "".$q."failed
(" . $this->db->errno . ") " . $this->db->error; exit; } return true; } function sqlBakup() { $tmp="/tmp/".rand(0,time())."_".date("d_m_y__H_i").".sql.gz"; exec("mysqldump --host=".$this->dbserver." --user=".$this->dbuser." --password=".$this->dbpw." --opt ".$this->dbname." | gzip -c | cat > ".$tmp); return $tmp; } function sqlError() { return $this->db->error; } function sqlFetchArray() { return $this->res->fetch_assoc(); } function sqlGetData($query="") { if ($query) $this->sqlQuery($query); $c=0; $x=$this->sqlFetchArray(); while ($x) { $ret[$c]=$x; $x=$this->sqlFetchArray(); $c++; } return $ret; } function sqlClose() { } function sqlAddData($table,$data="") { $c=$this->getAsArray($data); for ($i=0;$i<$c;$i++) { if ($this->keys[$i]) { if (strlen($this->values[$i])) { $cols=$cols.$this->keys[$i].", "; $vals=$vals."'".@addslashes($this->values[$i])."', "; } } } $cols=substr($cols,0,strlen($cols)-2); $vals=substr($vals,0,strlen($vals)-2); $q="INSERT INTO ".$table." ($cols) VALUES ($vals)"; if ($this->sqlQuery($q)) { $this->sqlQuery("select id from ".$table." order by id DESC limit 0,1"); $id=$this->sqlFetchArray(); if ($id) return $id['id']; return true; } return false; } function sqlChangeData($table,$data) { if (!$data['id']) return $this->sqlAddData($table,$data); $this->sqlQuery("SELECT * FROM ".$table." WHERE id='".$data['id']."' LIMIT 0,1"); $tmp=$this->sqlFetchArray(); if (!$tmp) return $this->sqlAddData($table,$data); $id=$data['id']; $c=$this->getAsArray($data); $q="UPDATE ".$table." SET"; for ($i=0;$i<$c;$i++) { if ($this->keys[$i]!='id') $q="$q ".$this->keys[$i]."='".addslashes($this->values[$i])."', "; } $q=substr($q,0,strlen($q)-2); $q="$q WHERE id='$id'"; if ($this->sqlQuery($q)) { return $tmp['id']; } return false; } function sqlDeleteData($table,$id="") { if ($id) { $q="DELETE FROM ".$table." WHERE id='$id'"; if ($this->sqlQuery($q)) return true; return false; } } function createTable($name) { $q='CREATE TABLE IF NOT EXISTS '.$name.' (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MYISAM'; $this->sqlQuery($q); } function sqlExistsColumn($table,$name) { $this->sqlQuery("SELECT * FROM ".$table." LIMIT 0,1"); $finfo=$this->res->fetch_field(); while ($finfo) { if ($finfo->name==$name) return true; $finfo=$this->res->fetch_field(); } return false; } function sqlExistsTable($tabname) { $res=mysql_list_tables($this->dbname); if ($res) { while ($row=mysql_fetch_row($res)) { if (strtolower($tabname)==strtolower($row[0])) return true; } } } function sqlGetRows() { return $this->res->num_rows; } function install($def, $force_utf8 = false) { $table=$def['table']; $this->createTable($table); // Create Table and id-Column //$this->sqlQuery('ALTER TABLE `'.$table.'` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci'); foreach ($def['col'] as $key => $value) { if (!$this->sqlExistsColumn($table,$key) ) { $q="ALTER TABLE ".$table." ADD ".$key." ".$value; $this->sqlQuery($q); if($force_utf8 === true) { $q="ALTER TABLE ".$table." CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci"; $this->sqlQuery($q); } } } } } // End of Class ?>