mysql教程i 數據連接類
class Mydb extends mysqli
{
protected $_databasehost = 'localhost';
protected $_databaseuser = 'root';
protected $_databasepass = '123456';
protected $_databasename = 'test';
protected $_databaseport = '3306';
protected $_fetch_method = MYSQLI_ASSOC;
protected $_databasechar = 'utf8';
/**
* Enter description here...
*
* @param string $host
* @param string $user
* @param string $pass
* @param string $name
* @param int $port
* @param string $char
*/
function __construct($host='',$user='',$pass='',$name='',$port='',$char='')
{
$this->_databasehost = empty($host) ? $this->_databasehost : $host;
$this->_databaseuser = empty($user) ? $this->_databaseuser : $user;
$this->_databasepass = empty($pass) ? $this->_databasepass : $pass;
$this->_databasename = empty($name) ? $this->_databasename : $name;
$this->_databaseport = empty($port) ? $this->_databaseport : $port;
$this->_databasechar = empty($char) ? $this->_databasechar : $char;
try{
parent::__construct($this->_databasehost,$this->_databaseuser,
$this->_databasepass,$this->_databasename,
$this->_databaseport);
if(mysqli_connect_errno()){
die("服務器連接失敗");
}
if(!parent::set_charset($this->_databasechar)){
die("數據庫教程無法使用utf-8編碼");
}
}catch(Exception $e){
die($e->getMessage());
}
}
function selectDb($name)
{
$this->_databasename = empty($name) ? $this->_databasename : $name;
return $this->select_db($this->_databasename);
}
/**
* Enter description here...
*
*/
function __desstruct()
{
$this->close();
}
/**
* 設置返回值類型
*
* @param int $type
*/
function setFetchMethod($type=2)
{
switch($type)
{
case '1':
$this->_fetch_method = MYSQLI_NUM;
break;
case '2':
$this->_fetch_method = MYSQLI_ASSOC;
break;
case '3':
$this->_fetch_method = MYSQLI_BOTH;
break;
default:
$this->_fetch_method = MYSQLI_ASSOC;
break;
}
}
/**
* 插入前轉義
*
* @param string|array $str
* @return string
*/
function quoteInto($str)
{
if(is_array($str)){
foreach ($str as $key=>$val){
$str[$key] = $this->real_escape_string($val);
}
}else{
$str = $this->real_escape_string($str);
}
return $str;
}
/**
* 插入
*
* @param string $table
* @param array $fields
* @return int
* @deprecated 鍵名為插入的字段值 鍵值為插入的值
*/
function insert($table,$fields)
{
if(empty($table)){return false;}
if(empty($fields)){return false;}
$insertFields = @implode(',',@array_keys($fields));
$arrayValues = @array_values($fields);
$result = $this->quoteInto($arrayValues);
$insertValues = "'".@implode("','",$result)."'";
$sql = "INSERT INTO ".$table."(".$insertFields.")
VALUES(".$insertValues.")";
$this->query($sql);
return $this->insert_id;
}
/**
* 更新
*
* @param string $table
* @param array $fields
* @param string $where
* @return null
* @deprecated 鍵名為插入的字段值 鍵值為插入的值
*/
function update($table,$fields,$where)
{
if(empty($table)){return false;}
if(empty($fields)){return false;}
if(empty($where)){return false;}
foreach($fields as $key=>$v)
{
$condition[] = "{$key}='".$this->quoteInto($v)."'";
}
$sql = "UPDATE ".$table."
SET ".implode(',',$condition)." WHERE ".$where;
$this->query($sql);
}
/**
* 獲取一條數據
*
* @param string $sql
* @return array 一維數組
*/
function fetchRow($sql)
{
$result = $this->query($sql);
$row = $result->fetch_array($this->_fetch_method);
return $row;
}
/**
* 獲取全部數據
*
* @param string $sql
* @return array 二維數組
*/
function fetchAll($sql)
{
$result = $this->query($sql);
while($row = $result->fetch_array($this->_fetch_method))
{
$data[] = $row;
}
return $data;
}
function fetchFree()
{
return $this->free_result();
}
/**
* Enter description here...
*
* @param string $type
* @return unknown
*/
function getVersion($type='server')
{
switch($type){
case 'server':
$result = $this->server_version;
break;
case 'client':
$result = $this->client_version;
break;
default:
$result = $this->server_version;
break;
}
return $result;
}
/**
* Enter description here...
*
* @param string $type
* @return unknown
*/
function getInfo($type='server')
{
switch($type){
case 'server':
$result = $this->server_info;
break;
case 'client':
$result = $this->client_info;
break;
default:
$result = $this->server_info;
break;
}
return $result;
}
}