📄 dbmysql.php
字号:
<?php
class dbmysql extends DB{
// __construct
public function __construct(){
//模块没加载
if(!extension_loaded('mysql')){
throwError('mysql:'.L('error.nodbext'));
}
}
// connect
public function connect(){
if ($this->_conn) { return $this->_conn; }
// 连接数据库
$this->_conn = @mysql_connect($this->_config['db_host'].':'.$this->_config['db_port'],$this->_config['db_user'],$this->_config['db_pwd']);
// 验证连接是否正确
if (!$this->_conn) {
throwError(L('error.dblink'));
}
return $this->_conn;
}
// select
public function select($db=''){
// 验证连接是否正确
if (!$this->_conn) { $this->connect();}
$db = empty($db) ? $this->_config['db_name'] : $db;
$select = mysql_select_db($db,$this->_conn);
// 选择数据库
if (!$select) {
throwError(L('error.selectdb').$this->_config['db_name']);
}
if (version_compare($this->version(), '4.1', '<' )) {
throwError(L('error.nodbver'));
}
mysql_query("SET NAMES '".C('db_lang')."'", $this->_conn);
return $this->_conn;
}
/**
* @ 执行查询(SQL, 查询方式)
* 返回资源 $RS
*/
private function execute($sql, $func){
// 验证连接是否正确
if (!$this->_conn) { $this->connect();}
$sql = $this->Xprefix($sql);
$this->_sql = $sql;
// SQL 调试模式
if(C('debug_sql')){
$_SESSION['debug_sql'][] = now().": ". $this->_sql;
}
// 执行查询
if(!$this->_rs = $func($sql, $this->_conn)){
throwError('MySQL Query Error:<br/>SQL:'.$sql."<br>".$this->error(),$this->errno());
}
return $this->_rs;
}
// 取得最近一次 INSERT,UPDATE 或 DELETE 查询所影响的记录行数
public function affected_rows(){
return mysql_affected_rows($this->_conn);
}
// query
public function query($sql,$bind=''){
if ((string)$bind!='') { $sql = $this->quoteInto($sql,$bind); }
return $this->execute($sql,'mysql_query');
}
// exec
public function exec($sql,$bind=''){
if ((string)$bind!='') { $sql = $this->quoteInto($sql,$bind); }
return $this->execute($sql,'mysql_unbuffered_query');
}
// fetch (fetch_type) 0:对像 1:关联数组 2:数字数组 3:BOTH
public function fetch($l1=null){
$l1 = is_null($l1) ? C('fetch_type') : $l1;
if($l1 == 0){
return mysql_fetch_object($this->_rs);
} else {
return mysql_fetch_array($this->_rs, $l1);
}
}
// count(table, where)
public function count($l1='', $l2=null){
if (empty($l1)){
return mysql_num_rows($this->_rs);
} else {
$l2 = empty($l2) ? '' : ' WHERE '.$this->Xwhere($l2);
return $this->result('select COUNT(*) from '.$this->quoteKey($l1).$l2);
}
}
/**
* @ resault(sql, num)
* 用于返回SQL 结果集中的一个单元
*/
public function result($l1='', $l2=0){
if(!empty($l1)){
$this->query($l1);
}
if(is_resource($this->_rs)){
$l1 = $this->fetch(2);
return $l1[$l2];
} else {
return false;
}
}
// error
public function error() {
return (($this->_conn) ? mysql_error($this->_conn) : mysql_error());
}
// errno
public function errno() {
return intval(($this->_conn) ? mysql_errno($this->_conn) : mysql_errno());
}
// version
public function version(){
return mysql_get_server_info($this->_conn);
}
// lastId
public function lastId() {
return ($I1 = mysql_insert_id($this->_conn)) >= 0 ? $I1 : $this->result("SELECT last_insert_id();");
}
// free
public function free(){
if (is_resource($this->_rs)) {
return mysql_free_result($this->_rs);
}
}
// close
public function close(){
if (is_resource($this->_conn)) {
return mysql_close($this->_conn);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -