日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

一个关于php使用pdo方式进行数据库连接和处理的类

發布時間:2024/9/20 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个关于php使用pdo方式进行数据库连接和处理的类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • <?php???
  • /**?
  • ????@DB?Operates?For?PDO?
  • ????@author:MeeeeN?
  • ????@date:2015-10-22?22:40:32?
  • **/??
  • ??
  • ????//定義數據庫信息??
  • ??????
  • ????header("Content-type:text/html;?charset=utf-8");??
  • ??
  • ????define('DB_HOST',?'localhost');??
  • ????define('DB_USER',?'root');??
  • ????define('DB_PWD',?'');??
  • ????define('DB_NAME',?'lesson');??
  • ??
  • ????class?DBPDO?{??
  • ??
  • ????????private?static?$instance;?????????
  • ????????public?$dsn;?????????
  • ????????public?$dbuser;?????????
  • ????????public?$dbpwd;?????????
  • ????????public?$sth;?????????
  • ????????public?$dbh;???
  • ??
  • ????????//初始化??
  • ????????function?__construct()?{??
  • ????????????$this->dsn?=?'mysql:host='.DB_HOST.';dbname='.DB_NAME;??
  • ????????????$this->dbuser?=?DB_USER;??
  • ????????????$this->dbpwd?=?DB_PWD;??
  • ????????????$this->connect();??
  • ????????????$this->dbh->query("SET?NAMES?'UTF8'");??
  • ????????????$this->dbh->query("SET?TIME_ZONE?=?'+8:00'");??
  • ????????}??
  • ??
  • ????????//連接數據庫??
  • ????????public?function?connect()?{??
  • ????????????try?{??
  • ????????????????$this->dbh?=?new?PDO($this->dsn,?$this->dbuser,?$this->dbpwd);??
  • ????????????}??
  • ????????????catch(PDOException?$e)?{??
  • ????????????????exit('連接失敗:'.$e->getMessage());??
  • ????????????}??
  • ????????}??
  • ??
  • ????????//獲取表字段??
  • ????????public?function?getFields($table='vista_order')?{??
  • ????????????$this->sth?=?$this->dbh->query("DESCRIBE?$table");??
  • ????????????$this->getPDOError();??
  • ????????????$this->sth->setFetchMode(PDO::FETCH_ASSOC);??
  • ????????????$result?=?$this->sth->fetchAll();??
  • ????????????$this->sth?=?null;??
  • ????????????return?$result;??
  • ????????}??
  • ??
  • ????????//插入數據??
  • ????????public?function?insert($sql)?{??
  • ????????????if($this->dbh->exec($sql))?{??
  • ????????????????$this->getPDOError();??
  • ????????????????return?$this->dbh->lastInsertId();??
  • ????????????}??
  • ????????????return?false;??
  • ????????}??
  • ??
  • ????????//刪除數據??
  • ????????public?function?delete($sql)?{??
  • ????????????if(($rows?=?$this->dbh->exec($sql))?>?0)?{??
  • ????????????????$this->getPDOError();??
  • ????????????????return?$rows;??
  • ????????????}??
  • ????????????else?{??
  • ????????????????return?false;??
  • ????????????}??
  • ????????}??
  • ??
  • ????????//更改數據??
  • ????????public?function?update($sql)?{??
  • ????????????if(($rows?=?$this->dbh->exec($sql))?>?0)?{??
  • ????????????????$this->getPDOError();??
  • ????????????????return?$rows;??
  • ????????????}??
  • ????????????return?false;??
  • ????????}??
  • ??
  • ????????//獲取數據??
  • ????????public?function?select($sql)?{??
  • ????????????$this->sth?=?$this->dbh->query($sql);??
  • ????????????$this->getPDOError();??
  • ????????????$this->sth->setFetchMode(PDO::FETCH_ASSOC);??
  • ????????????$result?=?$this->sth->fetchAll();??
  • ????????????$this->sth?=?null;??
  • ????????????return?$result;??
  • ????????}??
  • ??
  • ????????//獲取數目??
  • ????????public?function?count($sql)?{??
  • ????????????$count?=?$this->dbh->query($sql);??
  • ????????????$this->getPDOError();??
  • ????????????return?$count->fetchColumn();??
  • ????????}??
  • ??
  • ????????//獲取PDO錯誤信息??
  • ????????private?function?getPDOError()?{??
  • ????????????if($this->dbh->errorCode()?!=?'00000')?{??
  • ????????????????$error?=?$this->dbh->errorInfo();??
  • ????????????????exit($error[2]);??
  • ????????????}??
  • ????????}??
  • ??
  • ????????//關閉連接??
  • ????????public?function?__destruct()?{??
  • ????????????$this->dbh?=?null;??
  • ????????}??
  • ????}??
  • ??
  • ????//eg:?an?example?for?operate?select??
  • ??
  • ????$test?=?new?DBPDO;??
  • ??
  • ????$sql?=?"SELECT?*?FROM?`vista_order`?WHERE?`id`!=100?";??
  • ??
  • ????$rs?=?$test->select($sql);??
  • ??
  • ????print_r($rs);??
  • ??????
  • ??
  • ??
  • ??
  • ??
  • ?>??
  • 這是之前研究了一段時間pdo后所寫出來的一個pdo數據庫相關操作類(比較懶,一直沒更新博客),參考了一些網上的相關文章,但是感覺很多要么寫得有錯誤,要么很啰嗦,所以自己搞了個,其實本來我是一直是用MySQL類連接的,但是升級了PHP版本后發現不支持mysql方式連接了,又感覺mysqli比較啰嗦,所以索性改為用pdo,其實基本功能來說的話,這個類中construct,connection,destruct三個function就足夠了,不過方便快速使用的話還是多寫了一些function,個人感覺這個類的可移植性還是蠻高的,最后有使用的例子,基本上引用DBPDO類之后,只要自己寫好sql語句,增刪改查就都可以實現了

    來源:http://blog.csdn.net/meeeen7/article/details/52136474


    總結

    以上是生活随笔為你收集整理的一个关于php使用pdo方式进行数据库连接和处理的类的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。