国产精品成人VA在线观看,亚洲日韩在线中文字幕综合,亚洲AV电影天堂男人的天堂,久久人人爽人人爽人人av东京热

News新聞

業(yè)界新聞動態(tài)、技術(shù)前沿
Who are we?

您的位置:首頁      樂道系統(tǒng)FAQ      php mysql數(shù)據(jù)庫操作類(實(shí)例講解)

php mysql數(shù)據(jù)庫操作類(實(shí)例講解)

標(biāo)簽: 發(fā)布日期:2017-08-06 00:00:00 242

接著稍微說說整體的思路。整個類的封裝,包含一個連接數(shù)據(jù)庫的私有屬性$conn和若干操作函數(shù)。$conn在對象實(shí)例化的時候,由構(gòu)造函數(shù)處理傳入的參數(shù)后返回一個資源型的連接句柄。而后即可通過調(diào)用該實(shí)例化的對象的相應(yīng)方法對數(shù)據(jù)庫進(jìn)行增刪查改的操作。

talk less and show code:

<?php
/** 
*以下代碼用于數(shù)據(jù)庫操作類的封裝
* 
* @author rex<rex.sp.li@aliyun.com> 
* @version 1.0
* @since 2015
*/


class Mysql{

//數(shù)據(jù)庫連接返回值
private $conn;

/**
* [構(gòu)造函數(shù),返回值給$conn]
* @param [string] $hostname [主機(jī)名]
* @param [string] $username[用戶名]
* @param [string] $password[密碼]
* @param [string] $dbname[數(shù)據(jù)庫名]
* @param [string] $charset[字符集]
* @return [null]

*/

function __construct($hostname,$username,$password,$dbname,$charset='utf8'){
  $conn = @mysql_connect($hostname,$username,$password);
  if(!$conn){
    echo '連接失敗,請聯(lián)系管理員';
    exit;
  }
  $this->conn = $conn;
  $res = mysql_select_db($dbname);
  if(!$res){
  echo '連接失敗,請聯(lián)系管理員';
  exit;
  }
  mysql_set_charset($charset);
}
function __destruct(){
  mysql_close();
}
/**
* [getAll 獲取所有信息]
* @param [string] $sql [sql語句]
* @return [array] [返回二維數(shù)組]
*/
function getAll($sql){
  $result = mysql_query($sql,$this->conn);
  $data = array();
  if($result && mysql_num_rows($result)>0){
    while($row = mysql_fetch_assoc($result)){
    $data[] = $row;
    }
  }
  return $data;
}
/**
* [getOne 獲取單條數(shù)據(jù)]
* @param [string] $sql [sql語句]
* @return [array] [返回一維數(shù)組]
*/
function getOne($sql){
  $result = mysql_query($sql,$this->conn);
  $data = array();
  if($result && mysql_num_rows($result)>0){
    $data = mysql_fetch_assoc($result);
  }
  return $data;
}

/**
* [getOne 獲取單條數(shù)據(jù)]
* @param [string] $table [表名]
* @param [string] $data [由字段名當(dāng)鍵,屬性當(dāng)鍵值的一維數(shù)組]
* @return [type] [返回false或者插入數(shù)據(jù)的id]
*/

function insert($table,$data){
  $str = '';
  $str .="INSERT INTO `$table` ";
  $str .="(`".implode("`,`",array_keys($data))."`) "; 
  $str .=" VALUES ";
  $str .= "('".implode("','",$data)."')";
  $res = mysql_query($str,$this->conn);
  if($res && mysql_affected_rows()>0){
      return mysql_insert_id();
  }else{
    return false;
  }
}
/**
* [update 更新數(shù)據(jù)庫]
* @param [string] $table [表名]
* @param [array] $data [更新的數(shù)據(jù),由字段名當(dāng)鍵,屬性當(dāng)鍵值的一維數(shù)組]
* @param [string] $where [條件,‘字段名'=‘字段屬性']
* @return [type] [更新成功返回影響的行數(shù),更新失敗返回false]
*/
function update($table,$data,$where){
  $sql = 'UPDATE '.$table.' SET ';
  foreach($data as $key => $value){
  $sql .= "`{$key}`='{$value}',";
  }
  $sql = rtrim($sql,',');
  $sql .= " WHERE $where";
  $res = mysql_query($sql,$this->conn);
  if($res && mysql_affected_rows()){
    return mysql_affected_rows();
  }else{
  return false;
  }
}

/**
* [delete 刪除數(shù)據(jù)]
* @param [string] $table [表名]
* @param [string] $where [條件,‘字段名'=‘字段屬性']
* @return [type] [成功返回影響的行數(shù),失敗返回false]
*/
function del($table,$where){
  $sql = "DELETE FROM `{$table}` WHERE {$where}";
  $res = mysql_query($sql,$this->conn);
  if($res && mysql_affected_rows()){
    return mysql_affected_rows();
  }else{
  return false;
  }
}
}