class db3 {
public $conn_id=false;
public $dbprefix;
function insert_id()
{
return @mysql_insert_id($this->conn_id);
}
/**
* Persistent database connection
*
* @accessprivate called by the base class
* @returnresource
*/
function db_connect()
{
return @mysql_connect('127.0.0.1', 'root', 'i5i5a7',true);
}
// --------------------------------------------------------------------
/**
* user add
*/
/**
* 转义数据库字符串
*
* @param string
* @return string
*/
public static function escapeString($str)
{
return mysql_real_escape_string($str);
}
/**
* \把传入的数据生成 SQL 语句需要的格式
*
* @param any 待转换的数据,如果是数组还可以使用运算符
* @param bool 设置为字段引用,例如用到运算
* @return string
*/
protected function validate($var, $setField = false)
{
if ($setField && is_array($var) && isset($var[0]) && isset($var[1]))
{
if ($var[1] === true)
{
return $var[0];
}
else if (in_array($var[1], array('+', '-', '*', '/')) && is_numeric($var[0]))
{
return "`$setField` {$var[1]} {$var[0]}";
}
}
if ('integer' == gettype($var))
{
return $var;
}
else if (is_bool($var))
{
return intval($var);
}
else if (is_array($var))
{
$var = serialize($var);
}
return "'" . $this->escapeString($var) . "'";
}
protected function freeResult($rs)
{
return @mysql_free_result($rs);
}
/**
* 发送SQL语句到mysql服务器
*
* @param string $sql
* @param 是否不使用mysql内部缓存机制缓存
* @return unknown
*/
protected function sendSql($sql, $noCache = false)
{
if ('' == $sql)
{
return false;
}
if(false === $this->conn_id)
{
$this->conn_id = $this->db_connect();
mysql_select_db('gamac', $this->conn_id);
mysql_query('SET NAMES UTF8');
}
$sql = str_replace('#HEADER#', $this->dbprefix, $sql);
if ($noCache)
{
$rs = mysql_unbuffered_query($sql, $this->conn_id);
}
else
{
$rs = mysql_query($sql, $this->conn_id);
}
if (false === $rs)
{
if(__DEBUG__)
{
error($sql . '
' . mysql_error($this->conn_id));
return false;
}
else
{
error('sqlerror');
}
}
return $rs;
}
protected function _getRow($rs)
{
$rs = (false === $rs) ? $this->resultId : $rs;
return mysql_fetch_row($rs);
;
}
/**
* 反回一条记录
*
* @param #resouce $rs
* @return uarray
*/
public function fetch($rs = false)
{
$rs = (false === $rs) ? $this->resultId : $rs;
return @mysql_fetch_assoc($rs);
}
/**
* 根据参数自动生成INSERT 或者 REPLACE语句
*
* @param string 表名
* @param array 数据
* @param string 插入还是替换(INSERT/REPLACE)默认插入
* @return string
*/
public function sqlInsert($table, $data, $stat = 'INSERT')
{
$fields = '';
$values = '';
foreach ($data as $key => $var)
{
$fields .= ", `$key`";
$values .= ', ' . $this->validate($var, true);
}
$fields = substr($fields, 2);
$values = substr($values, 2);
return "{$stat} INTO `$table` ($fields) VALUES ($values)";
}
/**
* 生成UPDATE语句
*
* @param string $table
* @param array $data
* @param string WHERE条件
* @return string
*/
public function sqlUpdate($table, $data, $where)
{
$values = '';
foreach ($data as $key => $var)
{
$values .= ", `$key` = " . $this->validate($var, $key);
}
$values = substr($values, 2);
return "UPDATE `$table` SET $values WHERE $where";
}
/**
* 生成DELETE语句
*
* @param unknown_type $table
* @param unknown_type $where
* @return unknown
*/
static public function sqlDelete($table, $where)
{
return "DELETE FROM `$table` WHERE $where";
}
/**
* 执行sql
*
* @param string SQL语句
* @param bool是否不使用mysql内部缓存机制缓存
* @return 资源或者插入主键
*/
public function queryData($sql, $noCache = false)
{
$rs = $this->sendSql($sql, $noCache);
$this->resultId = $rs;
return $rs;
}
/**
* 反回列数据
* @param string SQL语句
* @param integer第几列
* @param bool是否反回多列
* @param bool不采用mysql内部缓存
* @return integer Or array
*/
public function getRow($sql = '', $num = 0, $array = false, $noCache = 0)
{
if (true == $array)
{
$rs = $this->sendSql($sql, $noCache);
$data = array();
while ($t = $this->_getRow($rs))
{
$data[] = $t[$num];
}
}
else
{
if (!stripos($sql, 'LIMIT'))
{
$sql .= ' LIMIT 1';
}
$rs = $this->sendSql($sql, $noCache);
$data = $this->_getRow($rs);
$data = $data[$num];
}
$this->freeResult($rs);
return $data;
}
/**
* 取一条记录(第一条)
* @param SQL语句
* @return array
*/
public function getOne($sql = '', $noCache = 0)
{
$rs = $this->sendSql($sql, $noCache);
$data = $this->fetch($rs);
$this->freeResult($rs);
return $data;
}
/**
* 反回多条记录
* @param SQL
* @return array
*/
public function getAll($sql = '', $noCache = 0)
{
$data = array();
$rs = $this->sendSql($sql, $noCache);
while ($row = $this->fetch($rs))
{
$data[] = $row;
}
$this->freeResult($rs);
return $data;
}
/**
* 插入记录到数据库
* @param 表名
* @param array 数组
* @return 若成功则返回新记录的主键 否则返回false
*/
public function insertData($table, $data)
{
$sql = $this->sqlInsert($table, $data);
$result = $this->sendSql($sql);
if ($result)
{
return $this->insert_id();
}
else
{
return false;
}
}
/**
* 插入记录到数据库
* @param 表名
* @param array 数组
* @return 若成功则返回true 否则返回false
*/
public function insert($table, $data)
{
$sql = $this->sqlInsert($table, $data);
$result = $this->sendSql($sql);
}
/**
*执行替换
* @param 表名
* @param array 数组
* @return 若成功则返回true 否则返回false
*/
public function replaceData($table, $data)
{
$sql = $this->sqlInsert($table, $data, 'REPLACE');
$result = $this->sendSql($sql);
if ($result)
{
return true;
}
return false;
}
/**
* 执行更新操作
* @param $table 表名
* @param $data 数据
* @param $where 条件
* @return 反回影响记录数,错误反回false
*/
public function updateData($table, $data, $where)
{
$sql = $this->sqlUpdate($table, $data, $where);
$this->sendSql($sql);
return $this->offectedRows();
}
/**
* 反回最近一次操作影响的记录条数
*
* @return integer
*/
public function offectedRows()
{
return mysql_affected_rows($this->conn_id);
}
/**
* 执行删除操作
* @param $table 表名
* @param $where 条件
* @return 反回影响记录数,错误反回false
*/
public function deleteData($table, $where)
{
$sql = $this->sqlDelete($table, $where);
$this->sendSql($sql);
return mysql_affected_rows($this->conn_id);
}
}
/* End of file mysql_driver.php */
/* Location: ./system/database/drivers/mysql/mysql_driver.php */
一键复制
编辑
Web IDE
原始数据
按行查看
历史