最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡單的php數(shù)據(jù)庫操作類代碼(增,刪,改,查)

 更新時間:2013年04月08日 09:23:29   作者:  
這幾天準備重新學習,梳理一下知識體系,同時按照功能模塊劃分做一些東西。所以。mysql的操作成為第一個要點。我寫了一個簡單的mysql操作類,實現(xiàn)數(shù)據(jù)的簡單的增刪改查功能。

數(shù)據(jù)庫操縱基本流程為:

  1、連接數(shù)據(jù)庫服務器

  2、選擇數(shù)據(jù)庫

  3、執(zhí)行SQL語句

  4、處理結果集

  5、打印操作信息

  其中用到的相關函數(shù)有

•resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )  連接數(shù)據(jù)庫服務器
•resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] )  連接數(shù)據(jù)庫服務器,長連接
•int mysql_affected_rows ( [resource link_identifier] )取得最近一次與 link_identifier 關聯(lián)的 INSERT,UPDATE 或 DELETE 查詢所影響的記錄行數(shù)。
•bool mysql_close ( [resource link_identifier] )如果成功則返回 TRUE,失敗則返回 FALSE。
•int mysql_errno ( [resource link_identifier] )返回上一個 MySQL 函數(shù)的錯誤號碼,如果沒有出錯則返回 0(零)。
•string mysql_error ( [resource link_identifier] )返回上一個 MySQL 函數(shù)的錯誤文本,如果沒有出錯則返回 ''(空字符串)。如果沒有指定連接資源號,則使用上一個成功打開的連接從 MySQL 服務器提取錯誤信息。
•array mysql_fetch_array ( resource result [, int result_type] )返回根據(jù)從結果集取得的行生成的數(shù)組,如果沒有更多行則返回 FALSE。
•bool mysql_free_result ( resource result )釋放所有與結果標識符 result 所關聯(lián)的內存。
•int mysql_num_fields ( resource result )返回結果集中字段的數(shù)目。
•int mysql_num_rows ( resource result )返回結果集中行的數(shù)目。此命令僅對 SELECT 語句有效。要取得被 INSERT,UPDATE 或者 DELETE 查詢所影響到的行的數(shù)目,用 mysql_affected_rows()。
•resource mysql_query ( string query [, resource link_identifier] ) 向與指定的連接標識符關聯(lián)的服務器中的當前活動數(shù)據(jù)庫發(fā)送一條查詢。如果沒有指定 link_identifier,則使用上一個打開的連接。如果沒有打開的連接,本函數(shù)會嘗試無參數(shù)調用 mysql_connect() 函數(shù)來建立一個連接并使用之。查詢結果會被緩存
代碼如下:


復制代碼 代碼如下:

class mysql {

     private $db_host;       //數(shù)據(jù)庫主機
     private $db_user;       //數(shù)據(jù)庫登陸名
     private $db_pwd;        //數(shù)據(jù)庫登陸密碼
     private $db_name;       //數(shù)據(jù)庫名
     private $db_charset;    //數(shù)據(jù)庫字符編碼
     private $db_pconn;      //長連接標識位
     private $debug;         //調試開啟
     private $conn;          //數(shù)據(jù)庫連接標識
     private $msg = "";      //數(shù)據(jù)庫操縱信息

 //    private $sql = "";      //待執(zhí)行的SQL語句

     public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {
         $this->db_host = $db_host;
         $this->db_user = $db_user;
         $this->db_pwd = $db_pwd;
         $this->db_name = $db_name;
         $this->db_charset = $db_chaeset;
         $this->db_pconn = $db_pconn;
         $this->result = '';
         $this->debug = $debug;
         $this->initConnect();
     }

     public function initConnect() {
         if ($this->db_pconn) {
             $this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
         } else {
             $this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
         }
         if ($this->conn) {
             $this->query("SET NAMES " . $this->db_charset);
         } else {
             $this->msg = "數(shù)據(jù)庫連接出錯,錯誤編號:" . mysql_errno() . "錯誤原因:" . mysql_error();
         }
         $this->selectDb($this->db_name);
     }

     public function selectDb($dbname) {
         if ($dbname == "") {
             $this->db_name = $dbname;
         }
         if (!mysql_select_db($this->db_name, $this->conn)) {
             $this->msg = "數(shù)據(jù)庫不可用";
         }
     }

     public function query($sql, $debug = false) {
         if (!$debug) {
             $this->result = @mysql_query($sql, $this->conn);
         } else {

         }
         if ($this->result == false) {
             $this->msg = "sql執(zhí)行出錯,錯誤編號:" . mysql_errno() . "錯誤原因:" . mysql_error();
         }
 //        var_dump($this->result);
     }

     public function select($tableName, $columnName = "*", $where = "") {
         $sql = "SELECT " . $columnName . " FROM " . $tableName;
         $sql .= $where ? " WHERE " . $where : null;
         $this->query($sql);
     }

     public function findAll($tableName) {
         $sql = "SELECT * FROM $tableName";
         $this->query($sql);
     }

     public function insert($tableName, $column = array()) {
         $columnName = "";
         $columnValue = "";
         foreach ($column as $key => $value) {
             $columnName .= $key . ",";
             $columnValue .= "'" . $value . "',";
         }
         $columnName = substr($columnName, 0, strlen($columnName) - 1);
         $columnValue = substr($columnValue, 0, strlen($columnValue) - 1);
         $sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)";
         $this->query($sql);
         if($this->result){
             $this->msg = "數(shù)據(jù)插入成功。新插入的id為:" . mysql_insert_id($this->conn);
         }
     }

     public function update($tableName, $column = array(), $where = "") {
         $updateValue = "";
         foreach ($column as $key => $value) {
             $updateValue .= $key . "='" . $value . "',";
         }
         $updateValue = substr($updateValue, 0, strlen($updateValue) - 1);
         $sql = "UPDATE $tableName SET $updateValue";
         $sql .= $where ? " WHERE $where" : null;
         $this->query($sql);
         if($this->result){
             $this->msg = "數(shù)據(jù)更新成功。受影響行數(shù):" . mysql_affected_rows($this->conn);
         }
     }

     public function delete($tableName, $where = ""){
         $sql = "DELETE FROM $tableName";
         $sql .= $where ? " WHERE $where" : null;
         $this->query($sql);
         if($this->result){
             $this->msg = "數(shù)據(jù)刪除成功。受影響行數(shù):" . mysql_affected_rows($this->conn);
         }
     }

     public function fetchArray($result_type = MYSQL_BOTH){
         $resultArray = array();
         $i = 0;
         while($result = mysql_fetch_array($this->result, $result_type)){
             $resultArray[$i] = $result;
             $i++;
         }
         return $resultArray;
     }

 //    public function fetchObject(){
 //        return mysql_fetch_object($this->result);
 //    }

     public function printMessage(){
         return $this->msg;
     }

     public function freeResult(){
         @mysql_free_result($this->result);
     }

     public function __destruct() {
         if(!empty($this->result)){
             $this->freeResult();
         }
         mysql_close($this->conn);
     }
 }

調用代碼如下

復制代碼 代碼如下:

require_once 'mysql_V1.class.php';
 require_once 'commonFun.php';
 $db = new mysql('localhost', 'root', '', "test");

 //select    查
 $db->select("user", "*", "username = 'system'");
 $result = $db->fetchArray(MYSQL_ASSOC);
 print_r($result);
 dump($db->printMessage());

 //insert    增
 //$userInfo = array('username'=>'system', 'password' => md5("system"));
 //$db->insert("user", $userInfo);
 //dump($db->printMessage());

 //update    改
 //$userInfo = array('password' => md5("123456"));
 //$db->update("user", $userInfo, "id = 2");
 //dump($db->printMessage());

 //delete    刪
 //$db->delete("user", "id = 1");
 //dump($db->printMessage());

 //findAll   查詢全部
 $db->findAll("user");
 $result = $db->fetchArray();
 dump($result);

ps,個人比較喜歡tp的dump函數(shù),所以在commonFun.php文件中拷貝了友好打印函數(shù)。使用時將其改為print_r()即可。

相關文章

  • Smarty模板變量與調節(jié)器實例詳解

    Smarty模板變量與調節(jié)器實例詳解

    這篇文章主要介紹了Smarty模板變量與調節(jié)器,結合實例形式詳細分析了Smarty模板變量與調節(jié)器基本概念、分類、使用方法及相關操作注意事項,需要的朋友可以參考下
    2019-07-07
  • Linux中用PHP判斷程序運行狀態(tài)的2個方法

    Linux中用PHP判斷程序運行狀態(tài)的2個方法

    這篇文章主要介紹了Linux中用PHP判斷程序運行狀態(tài)的2個方法,需要的朋友可以參考下
    2014-05-05
  • CI框架網(wǎng)頁緩存簡單用法分析

    CI框架網(wǎng)頁緩存簡單用法分析

    這篇文章主要介紹了CI框架網(wǎng)頁緩存簡單用法,結合實例形式分析了CI框架網(wǎng)頁緩存的原理,以及開啟緩存、刪除緩存等操作技巧,需要的朋友可以參考下
    2018-12-12
  • PHP在網(wǎng)頁中動態(tài)生成PDF文件詳細教程

    PHP在網(wǎng)頁中動態(tài)生成PDF文件詳細教程

    這篇文章主要介紹了PHP在網(wǎng)頁中動態(tài)生成PDF文件詳細教程,本文用一個需求為引,詳細介紹每一步驟的做法,并配有大量圖片說明,需要的朋友可以參考下
    2014-07-07
  • Yii2結合Workerman的websocket示例詳解

    Yii2結合Workerman的websocket示例詳解

    這篇文章主要給大家介紹了關于Yii2結合Workerman的websocket的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2018-09-09
  • PHP中通過getopt解析GNU C風格命令行選項

    PHP中通過getopt解析GNU C風格命令行選項

    這篇文章主要介紹了PHP中通過getopt解析GNU C風格命令行選項,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • 利用PHP如何寫APP接口詳解

    利用PHP如何寫APP接口詳解

    很多朋友在開發(fā)時遇到過這樣的問題:后臺什么時候提供接口?怎么才提供一個接口,其他接口什么時候給出來?沒有接口我們前端怎么能做得了?那學完本篇就可以自己搞個接口來返回固定的死數(shù)據(jù)來測試了!
    2016-08-08
  • PHP設計模式之觀察者模式實例

    PHP設計模式之觀察者模式實例

    一個對象通過添加一個方法(該方法允許另一個對象,即觀察者 注冊自己)使本身變得可觀察。當可觀察的對象更改時,它會將消息發(fā)送到已注冊的觀察者。通過本文給大家介紹PHP設計模式之觀察者模式,需要的朋友參考下
    2016-02-02
  • PHP判斷json格式是否正確的實現(xiàn)代碼

    PHP判斷json格式是否正確的實現(xiàn)代碼

    本文給大家分享PHP判斷json格式是否正確的實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-09-09
  • php實現(xiàn)數(shù)組重復數(shù)字統(tǒng)計實例

    php實現(xiàn)數(shù)組重復數(shù)字統(tǒng)計實例

    在本篇文章里我們給大家?guī)硪粋€關于php實現(xiàn)數(shù)組重復數(shù)字統(tǒng)計的實例,有用到的朋友們參考下。
    2018-09-09

最新評論

普陀区| 敖汉旗| 吴旗县| 准格尔旗| 昌图县| 临清市| 儋州市| 千阳县| 栾川县| 曲阜市| 武冈市| 安阳县| 巴马| 交口县| 合作市| 张家口市| 徐闻县| 土默特左旗| 永丰县| 蓝田县| 枣阳市| 凤城市| 红安县| 房产| 墨玉县| 宾川县| 金溪县| 滦南县| 揭阳市| 津南区| 霍邱县| 叶城县| 昌黎县| 铁力市| 黄石市| 洛川县| 南宫市| 陈巴尔虎旗| 石渠县| 酉阳| 江山市|