【www.gdgbn.com--mysql教程】

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;
 }
}

本文来源:http://www.gdgbn.com/shujuku/23716/