php mysql資料庫如何封裝類

來源:互聯網
上載者:User
本篇文章示範的代碼屬於較為簡單的資料庫封裝類,較適合初學,需要的朋友可以參考下

接著稍微說說整體的思路。整個類的封裝,包含一個串連資料庫的私人屬性$conn和若干操作函數。$conn在對象執行個體化的時候,由建構函式處理傳入的參數後返回一個資源型的串連控制代碼。而後即可通過調用該執行個體化的對象的相應方法對資料庫進行增刪查改的操作。

talk less and show code:

<?php/** *以下代碼用於資料庫操作類的封裝* * @author rex<rex.sp.li@aliyun.com> * @version 1.0* @since 2015*/class Mysql{//資料庫連接傳回值private $conn;/*** [建構函式,傳回值給$conn]* @param [string] $hostname [主機名稱]* @param [string] $username[使用者名稱]* @param [string] $password[密碼]* @param [string] $dbname[資料庫名]* @param [string] $charset[字元集]* @return [null]*/function __construct($hostname,$username,$password,$dbname,$charset='utf8'){  $conn = @mysql_connect($hostname,$username,$password);  if(!$conn){    echo '串連失敗,請聯絡管理員';    exit;  }  $this->conn = $conn;  $res = mysql_select_db($dbname);  if(!$res){  echo '串連失敗,請聯絡管理員';  exit;  }  mysql_set_charset($charset);}function __destruct(){  mysql_close();}/*** [getAll 擷取所有資訊]* @param [string] $sql [sql語句]* @return [array] [返回二維數組]*/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 擷取單條資料]* @param [string] $sql [sql語句]* @return [array] [返回一維數組]*/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 擷取單條資料]* @param [string] $table [表名]* @param [string] $data [由欄位名當鍵,屬性當索引值的一維數組]* @return [type] [返回false或者插入資料的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 更新資料庫]* @param [string] $table [表名]* @param [array] $data [更新的資料,由欄位名當鍵,屬性當索引值的一維數組]* @param [string] $where [條件,‘欄位名'=‘欄位屬性']* @return [type] [更新成功返回影響的行數,更新失敗返回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 刪除資料]* @param [string] $table [表名]* @param [string] $where [條件,‘欄位名'=‘欄位屬性']* @return [type] [成功返回影響的行數,失敗返回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;  }}}

執行個體化類:

<?php//包含資料庫操作類檔案include 'mysql.class.php';//設定傳入參數$hostname='localhost';$username='root';$password='123456';$dbname='aisi';$charset = 'utf8';//執行個體化對象$db = new Mysql($hostname,$username,$password,$dbname);//擷取一條資料$sql = "SELECT count(as_article_id) as count FROM as_article where as_article_type_id=1";$count = $db->getOne($sql);//擷取多條資料$sql = "SELECT * FROM as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit";$service = $db->getAll($sql);//插入資料$arr = array('as_article_title'=>'資料庫操作類','as_article_author'=>'rex',);$res = $db->insert('as_article',$arr);//更新資料$arr = array('as_article_title'=>'執行個體化對象','as_article_author'=>'Lee',);$where = "as_article_id=1";$res = $db->update('as_article',$arr,$where);//刪除資料$where = "as_article_id=1";$res = $db->del('as_article',$where);?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.