濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP封裝的mysqli數(shù)據(jù)庫操作類示例

PHP封裝的mysqli數(shù)據(jù)庫操作類示例

熱門標(biāo)簽:十堰ai電話機(jī)器人效果怎么樣 安陽自動外呼系統(tǒng)價格是多少 地圖標(biāo)注風(fēng)向標(biāo) 電梯外呼線路板維修視頻 芒果電銷機(jī)器人 上海公司外呼系統(tǒng)線路 臨沂智能電銷機(jī)器人軟件 銀川ai電話機(jī)器人 浙江外呼電話系統(tǒng)軟件

本文實(shí)例講述了PHP封裝的mysqli數(shù)據(jù)庫操作類。分享給大家供大家參考,具體如下:

由于PHPMySQL操作在PHP5.4以下被棄用而推薦使用mysqli(MySQL Improvement),這里是在我原有的MySQL數(shù)據(jù)庫操作類的基礎(chǔ)上加以改進(jìn)而編寫出的MySQLI數(shù)據(jù)庫操作類,整體代碼與MySQL數(shù)據(jù)庫操作類大致相同,直接實(shí)例化db_class即可

?php
class db_class{
    public $db_url; //連接地址
    public $db_username; //連接名
    public $db_userpassword; //連接密碼
    public $db_name; //數(shù)據(jù)庫名
    public $db_tablename; //表名
    public $db_conn; //數(shù)據(jù)庫連接
    public $db_order;
    public $db_limit;
    public function db_getconn(){ //連接數(shù)據(jù)庫
        $this->db_conn= mysqli_connect($this->db_url,$this->db_username,$this->db_userpassword,$this->db_name);
        if (!$this->db_conn)
        {
      echo "連接 MySQL 失敗: " . mysqli_connect_error();
    }
    }
    public function __construct($db_url,$db_username,$db_userpassword,$db_name){ //構(gòu)造方法賦值
    $this->db_url=$db_url;
        $this->db_username=$db_username;
        $this->db_userpassword=$db_userpassword;
        $this->db_name=$db_name;
        $this->db_order="";
        $this->db_limit="";
        $this->db_getconn();
    mysqli_query($this->db_conn,'set names utf8');
    }
    public function db_settablename($db_tablename){ //設(shè)置表名
        $this->db_tablename=$db_tablename;
    }
    public function db_setorder($str){ //排序操作
        $this->db_order="order by $str";
    }
    public function db_setlimit($start,$end){ //分頁操作
     $this->db_limit="limit $start,$end";
    }
    public function db_select($typearr="",$where=""){ //查詢操作
        if(empty($typearr)){
            $typearr="*";
        }
        else{
            $typearr=implode(",",$typearr);
        }
        if(empty($where)){
            $where="";
        }else{
            $where="where ".$where;
        }
        $arr=array();
        $sql="select $typearr from $this->db_tablename $where $this->db_order $this->db_limit ";
        $result = mysqli_query($this->db_conn,$sql);
        while($row = $result->fetch_array()) {
            $arr[]=$row;
        }
        return $arr;
    }
    public function db_update($typearr,$valuearr,$where=""){ //更新操作
    $sql="";
        if(empty($where)){
            $where="";
        }else{
            $where=" where ".$where;
        }
        $sql.="update $this->db_tablename set ";
         foreach ($typearr as $key=>$value){
             if(count($typearr)-1==$key){
                 $sql.=$value."='".$valuearr[$key]."'";
             }else{
                 $sql.=$value."='".$valuearr[$key]."'".",";
             }
         }
        $sql.=$where;
    mysqli_query($this->db_conn,$sql);
    }
    public function db_delete($typestr,$valuestr){ //刪除操作
    $sql="delete from $this->db_tablename WHERE $typestr=$valuestr";
    mysqli_query($this->db_conn,$sql);
    }
    public function db_insert($typearr,$valuearr){ //插入操作
        $sql="insert into $this->db_tablename(".implode(",", $typearr).") values(".implode(",", $valuearr).")";
    mysqli_query($this->db_conn,$sql);
    }
    public function __destruct(){ //析構(gòu)方法關(guān)閉連接
        mysqli_close($this->db_conn);
    }
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • php封裝db類連接sqlite3數(shù)據(jù)庫的方法實(shí)例
  • php db類庫進(jìn)行數(shù)據(jù)庫操作
  • PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類【定義與用法】
  • PHP數(shù)據(jù)庫表操作的封裝類及用法實(shí)例詳解
  • PHP封裝的PDO數(shù)據(jù)庫操作類實(shí)例
  • PHP數(shù)據(jù)庫處理封裝類實(shí)例
  • php簡單數(shù)據(jù)庫操作類的封裝
  • PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫操作類與用法示例
  • PHP模型Model類封裝數(shù)據(jù)庫操作示例
  • PHP封裝的數(shù)據(jù)庫模型Model類完整示例【基于PDO】
  • PHP封裝類似thinkphp連貫操作數(shù)據(jù)庫Db類與簡單應(yīng)用示例

標(biāo)簽:寧夏 武威 常州 吐魯番 遂寧 徐州 荊門 遵義

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP封裝的mysqli數(shù)據(jù)庫操作類示例》,本文關(guān)鍵詞  PHP,封,裝的,mysqli,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP封裝的mysqli數(shù)據(jù)庫操作類示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP封裝的mysqli數(shù)據(jù)庫操作類示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    尼勒克县| 南溪县| 大丰市| 定西市| 石景山区| 永寿县| 开江县| 普安县| 常德市| 崇义县| 杨浦区| 海原县| 喀什市| 兴安县| 宣恩县| 北辰区| 乐清市| 四会市| 靖边县| 民县| 邯郸县| 淳安县| 略阳县| 安新县| 海伦市| 万安县| 荥经县| 嘉峪关市| 金湖县| 赞皇县| 佛学| 贵定县| 凤凰县| 霍邱县| 昔阳县| 仁寿县| 揭西县| 洛浦县| 建宁县| 天全县| 房产|