public function tryConnect()
{
//connect
- $this->dbhdl=mysql_connect($this->server,$this->user,$this->pass);
+ $this->dbhdl=mysqli_connect($this->server,$this->user,$this->pass,$this->dbname);
if($this->dbhdl===false)
die("Unable to connect to database system. Giving up.");
- //select DB
- if(mysql_query("use $this->dbname",$this->dbhdl)===false)
- die("Unable to select database \"$this->dbname\". Giving up.");
- //TODO: select Unicode; TODO: fix it to be configurable
- //if(mysql_query("SET NAMES 'utf8'"))
- // die("cannot set character set to utf-8");
+ //select Unicode; TODO: fix it to be configurable
+ if(mysqli_query($this->dbhdl,"SET NAMES 'utf8'")===false)
+ die("cannot set character set to utf-8");
}
public function haveTable($tnm)
{
- $res=mysql_query("select * from ".$this->tableName($tnm)." where 1=2",$this->dbhdl);
+ $res=mysqli_query($this->dbhdl,"select * from ".$this->tableName($tnm)." where 1=2");
if($res===false)return false;
- mysql_free_result($res);
+ mysqli_free_result($res);
return true;
}
public function beginTransaction()
{
- return mysql_query("BEGIN");
+ return mysqli_query($this->dbhdl,"BEGIN");
}
public function commitTransaction()
{
- return mysql_query("COMMIT");
+ return mysqli_query($this->dbhdl,"COMMIT");
}
public function rollbackTransaction()
{
- return mysql_query("ROLLBACK");
+ return mysqli_query($this->dbhdl,"ROLLBACK");
}
public function select($table,$cols,$where)
{
$query="SELECT $cols FROM ".$this->tableName($table);
if($where!="")$query.=" WHERE ".$where;
- $res=mysql_query($query);
+ $res=mysqli_query($this->dbhdl,$query);
if($res===false)return false;
- $nr=mysql_num_rows($res);
+ $nr=mysqli_num_rows($res);
$ret=array();
for($i=0;$i<$nr;$i++){
- $ret[]=mysql_fetch_array($res,MYSQL_BOTH);
+ $ret[]=mysqli_fetch_array($res,MYSQLI_BOTH);
}
- mysql_free_result($res);
+ mysqli_free_result($res);
return $ret;
}
protected function createTable($tn,$t)
{
- return mysql_query($this->sqlCreateTable($tn,$t)." engine=".$this->engine);
+ return mysqli_query($this->dbhdl,$this->sqlCreateTable($tn,$t)." engine=".$this->engine);
}
protected function tableName($tn)
public function insert($table,array $values)
{
- $res=mysql_query($this->sqlInsert($table,$values));
+ $res=mysqli_query($this->dbhdl,$this->sqlInsert($table,$values));
if($res===false)return false;
global $dbScheme;
$seq=$dbScheme->hasSequence($table);
if($seq!==false){
if(isset($values[$seq]))return $values[$seq];
- $res=mysql_query("select LAST_INSERT_ID()");
- if(mysql_num_rows($res)>0){
- $row=mysql_fetch_array($res);
+ $res=mysqli_query($this->dbhdl,"select LAST_INSERT_ID()");
+ if(mysqli_num_rows($res)>0){
+ $row=mysqli_fetch_array($res);
$ret=$row[0];
}else{
$ret=true;
}
- mysql_free_result($res);
+ mysqli_free_result($res);
return $ret;
}else{
return true;
public function update($table,array $values,$where)
{
- $res=mysql_query($this->sqlUpdate($table,$values,$where));
- if($res!==false)return mysql_affected_rows();
+ $res=mysqli_query($this->dbhdl,$this->sqlUpdate($table,$values,$where));
+ if($res!==false)return mysqli_affected_rows($this->dbhdl);
else return false;
}
public function deleteRows($table,$where)
{
- mysql_query($this->sqlDelete($table,$where));
+ mysqli_query($this->dbhdl,$this->sqlDelete($table,$where));
}
public function lastError()
{
- return mysql_error();
+ return mysqli_error($this->dbhdl);
}
- /**escapes strings; it uses mysql_escape_string and encloses the value in ''*/
+ /**escapes strings; it uses mysqli_escape_string and encloses the value in ''*/
public function escapeString($s)
{
if($s === false) return "NULL";
- return "'".mysql_real_escape_string($s)."'";
+ return "'".mysqli_real_escape_string($this->dbhdl,$s)."'";
}
};
\ No newline at end of file