<?php
class MYSQL_DB extends DB
{
public function connect()
{
$this->connection = mysql_connect($this->host,$this->username,$this->password) or die($this->error_message);
mysql_select_db($this->database,$this->connection) or die($this->error_message);
return;
}
public function use_db($dbn)
{
mysql_select_db($dbn,$this->connection) or die($this->error_message);
$this->database = $dbn;
}
public function escape_str($str,$quotes=0,$exchars = '')
{
if (is_array($str))
{
$str = array_map(array('MYSQL_DB','escape_str'),$str);
}
else
{
if (get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
if (!is_numeric($str)) {
$str = mysql_real_escape_string($str);
$str = str_replace("\r",chr(92).'r',$str);
$str = str_replace("\n",chr(92).'n',$str);
if (!empty($exchars))
for ($i=0; $i<strlen($exchars); $i++)
$str = str_replace($exchars[$i],chr(92).$exchars[$i],$str);
if ($quotes)
$str = "'".$str."'";
}
}
return $str;
}
public function table_names()
{
$ret = array();
$sql = "SHOW TABLES";
$res = mysql_query($sql,$this->connection);
while ($data = mysql_fetch_array($res))
{
$ret[] = $data[0];
}
return $ret;
}
public function fetch_page_rows($table,$where='1',$page=1,$rpp='50',$assoc=1,$order='')
{
$ret = array();
$limit = '';
$offset = 0;
$sql = "SELECT * FROM $table WHERE $where";
if (!empty($order))
$sql .= " ORDER BY $order";
$offset = ($page - 1) * $rpp;
$limit = $offset.','.$rpp;
$sql .= " LIMIT $limit";
$res = mysql_query($sql,$this->connection);
if ($res)
{
while ($data = $assoc ? mysql_fetch_assoc($res) : mysql_fetch_array($res))
{
$ret[] = $data;
}
}
$ret[] = $paging_code;
return $ret;
}
public function fetch_all($table,$where='1',$assoc=1,$order='',$limit='')
{
$ret = array();
$sql = "SELECT * FROM $table WHERE $where";
if (!empty($order))
$sql .= " ORDER BY $order";
if (!empty($limit))
$sql .= " LIMIT $limit";
$res = mysql_query($sql,$this->connection);
if ($res)
{
while ($data = $assoc ? mysql_fetch_assoc($res) : mysql_fetch_array($res))
{
$ret[] = $data;
}
}
return $ret;
}
public function backup($file_name='',$tables='*',$nocrdb=0)
{
$ret = '';
if ($nocrdb == 0)
$ret = 'CREATE DATABASE IF NOT EXISTS `'.$this->database.'`;'."\n\n".'USE `'.$this->database.'`;'."\n\n";
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$ret.= 'DROP TABLE IF EXISTS `'.$table.'`;';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$ret.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$ret.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\r",chr(92).'r',$row[$j]);
$row[$j] = str_replace("\n",chr(92).'n',$row[$j]);
if (isset($row[$j])) { $ret.= "'".$row[$j]."'" ; } else { $ret.= "''"; }
if ($j<($num_fields-1)) { $ret.= ','; }
}
$ret.= ");\n";
}
}
$ret.="\n\n\n";
}
if (!empty($file_name))
{
$fh = fopen($file_name,'w');
fwrite($fh,$ret);
fclose($fh);
return true;
}
else
return $ret;
}
public function table_info($table)
{
$tinfo = array();
$sql = "DESCRIBE $table";
$res = @mysql_query($sql,$this->connection);
while ($data = mysql_fetch_assoc($res))
{
$tinfo[] = $data;
}
return $tinfo;
}
public function table_columns($table)
{
$tinfo = array();
$sql = "DESCRIBE $table";
$res = @mysql_query($sql,$this->connection);
while ($data = mysql_fetch_assoc($res))
{
$tinfo[] = $data['Field'];
}
return $tinfo;
}
public function close()
{
mysql_close($this->connection);
$this->connection = '';
return;
}
public function query($qry)
{
return @mysql_query($qry,$this->connection);
}
public function row($res,$assoc=1)
{
if ($assoc)
return @mysql_fetch_assoc($res);
else
return @mysql_fetch_array($res);
}
public function fetch_query($qry,$assoc = 1)
{
$retval = array();
$tval = array();
$res = mysql_query($qry,$this->connection);
if ($res)
{
if ($assoc)
{
while ($data = mysql_fetch_assoc($res))
{
$retval[] = $data;
}
}
else
{
while ($data = mysql_fetch_array($res))
{
$retval[] = $data;
}
}
}
return $retval;
}
public function next_id($tname)
{
$res = mysql_query("SHOW TABLE STATUS LIKE '$tname'",$this->connection);
$data = mysql_fetch_assoc($res);
return $data['Auto_increment'];
}
public function last_id()
{
return mysql_insert_id($this->connection);
}
public function insert($table,$values=array(),$columns=array())
{
$sql = "INSERT INTO $table ";
if (!empty($columns))
{
$sql .= '(';
foreach ($columns as $col)
{
$sql .= "'$col',";
}
$sql = substr($sql,0,strlen($sql)-1);
$sql .= ') ';
}
if (empty($values))
{
for ($i=0; $i < count($columns); $i++)
{
$values[$i] = '';
}
}
$sql .= "VALUES(";
foreach ($values as $val)
{
$sql .= "'$val',";
}
$sql = substr($sql,0,strlen($sql)-1);
$sql .= ');';
return mysql_query($sql,$this->connection);
}
public function update($table,$values,$where,$limit=0)
{
$sql = "UPDATE $table SET ";
foreach ($values as $key => $val)
{
$sql .= $key." = '$val', ";
}
$sql = substr($sql,0,strlen($sql)-2);
$sql = $sql." WHERE $where";
if ($limit != 0)
$sql .= " LIMIT $limit";
return mysql_query($sql,$this->connection);
}
public function delete($table,$where,$limit=0)
{
$sql = "DELETE FROM $table WHERE $where";
if ($limit != 0)
$sql .= " LIMIT $limit";
return mysql_query($sql,$this->connection);
}
public function affected_rows()
{
return mysql_affected_rows($this->connection);
}
public function num_rows($res)
{
return mysql_num_rows($res);
}
public function num_fields($res)
{
return mysql_num_fields($res);
}
}
?>