zoukankan      html  css  js  c++  java
  • 简单php数据操纵类

      这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西。所以。mysql的操作成为第一个要点。我写了一个简单的mysql操纵类,实现数据的简单的增删改查功能。后期继续添加更多的东西。

      数据库操纵基本流程为:

      1、连接数据库服务器

      2、选择数据库

      3、执行SQL语句

      4、处理结果集

      5、打印操作信息

      其中用到的相关函数有

    • resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )  连接数据库服务器
    • resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] )  连接数据库服务器,长连接
    • int mysql_affected_rows ( [resource link_identifier] )取得最近一次与 link_identifier 关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。
    • bool mysql_close ( [resource link_identifier] )如果成功则返回 TRUE,失败则返回 FALSE
    • int mysql_errno ( [resource link_identifier] )返回上一个 MySQL 函数的错误号码,如果没有出错则返回 0(零)。
    • string mysql_error ( [resource link_identifier] )返回上一个 MySQL 函数的错误文本,如果没有出错则返回 ''(空字符串)。如果没有指定连接资源号,则使用上一个成功打开的连接从 MySQL 服务器提取错误信息。
    • array mysql_fetch_array ( resource result [, int result_type] )返回根据从结果集取得的行生成的数组,如果没有更多行则返回 FALSE
    • bool mysql_free_result ( resource result )释放所有与结果标识符 result 所关联的内存。
    • int mysql_num_fields ( resource result )返回结果集中字段的数目。
    • int mysql_num_rows ( resource result )返回结果集中行的数目。此命令仅对 SELECT 语句有效。要取得被 INSERT,UPDATE 或者 DELETE 查询所影响到的行的数目,用 mysql_affected_rows()
    • resource mysql_query ( string query [, resource link_identifier] ) 向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。如果没有指定 link_identifier,则使用上一个打开的连接。如果没有打开的连接,本函数会尝试无参数调用 mysql_connect() 函数来建立一个连接并使用之。查询结果会被缓存

    代码如下:

      1 class mysql {
      2 
      3     private $db_host;       //数据库主机
      4     private $db_user;       //数据库登陆名
      5     private $db_pwd;        //数据库登陆密码
      6     private $db_name;       //数据库名
      7     private $db_charset;    //数据库字符编码
      8     private $db_pconn;      //长连接标识位
      9     private $debug;         //调试开启
     10     private $conn;          //数据库连接标识
     11     private $msg = "";      //数据库操纵信息
     12 
     13 //    private $sql = "";      //待执行的SQL语句
     14 
     15     public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {
     16         $this->db_host = $db_host;
     17         $this->db_user = $db_user;
     18         $this->db_pwd = $db_pwd;
     19         $this->db_name = $db_name;
     20         $this->db_charset = $db_chaeset;
     21         $this->db_pconn = $db_pconn;
     22         $this->result = '';
     23         $this->debug = $debug;
     24         $this->initConnect();
     25     }
     26 
     27     public function initConnect() {
     28         if ($this->db_pconn) {
     29             $this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
     30         } else {
     31             $this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
     32         }
     33         if ($this->conn) {
     34             $this->query("SET NAMES " . $this->db_charset);
     35         } else {
     36             $this->msg = "数据库连接出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();
     37         }
     38         $this->selectDb($this->db_name);
     39     }
     40 
     41     public function selectDb($dbname) {
     42         if ($dbname == "") {
     43             $this->db_name = $dbname;
     44         }
     45         if (!mysql_select_db($this->db_name, $this->conn)) {
     46             $this->msg = "数据库不可用";
     47         }
     48     }
     49 
     50     public function query($sql, $debug = false) {
     51         if (!$debug) {
     52             $this->result = @mysql_query($sql, $this->conn);
     53         } else {
     54             
     55         }
     56         if ($this->result == false) {
     57             $this->msg = "sql执行出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();
     58         }
     59 //        var_dump($this->result);
     60     }
     61 
     62     public function select($tableName, $columnName = "*", $where = "") {
     63         $sql = "SELECT " . $columnName . " FROM " . $tableName;
     64         $sql .= $where ? " WHERE " . $where : null;
     65         $this->query($sql);
     66     }
     67 
     68     public function findAll($tableName) {
     69         $sql = "SELECT * FROM $tableName";
     70         $this->query($sql);
     71     }
     72 
     73     public function insert($tableName, $column = array()) {
     74         $columnName = "";
     75         $columnValue = "";
     76         foreach ($column as $key => $value) {
     77             $columnName .= $key . ",";
     78             $columnValue .= "'" . $value . "',";
     79         }
     80         $columnName = substr($columnName, 0, strlen($columnName) - 1);
     81         $columnValue = substr($columnValue, 0, strlen($columnValue) - 1);
     82         $sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)";
     83         $this->query($sql);
     84         if($this->result){
     85             $this->msg = "数据插入成功。新插入的id为:" . mysql_insert_id($this->conn);
     86         }
     87     }
     88 
     89     public function update($tableName, $column = array(), $where = "") {
     90         $updateValue = "";
     91         foreach ($column as $key => $value) {
     92             $updateValue .= $key . "='" . $value . "',";
     93         }
     94         $updateValue = substr($updateValue, 0, strlen($updateValue) - 1);
     95         $sql = "UPDATE $tableName SET $updateValue";
     96         $sql .= $where ? " WHERE $where" : null;
     97         $this->query($sql);
     98         if($this->result){
     99             $this->msg = "数据更新成功。受影响行数:" . mysql_affected_rows($this->conn);
    100         }
    101     }
    102     
    103     public function delete($tableName, $where = ""){
    104         $sql = "DELETE FROM $tableName";
    105         $sql .= $where ? " WHERE $where" : null;
    106         $this->query($sql);
    107         if($this->result){
    108             $this->msg = "数据删除成功。受影响行数:" . mysql_affected_rows($this->conn);
    109         }
    110     }
    111 
    112     public function fetchArray($result_type = MYSQL_BOTH){
    113         $resultArray = array();
    114         $i = 0;
    115         while($result = mysql_fetch_array($this->result, $result_type)){
    116             $resultArray[$i] = $result;
    117             $i++;
    118         }
    119         return $resultArray;
    120     }
    121     
    122 //    public function fetchObject(){
    123 //        return mysql_fetch_object($this->result);
    124 //    }
    125     
    126     public function printMessage(){
    127         return $this->msg;
    128     }
    129     
    130     public function freeResult(){
    131         @mysql_free_result($this->result);
    132     }
    133     
    134     public function __destruct() {
    135         if(!empty($this->result)){
    136             $this->freeResult();
    137         }
    138         mysql_close($this->conn);
    139     }
    140 }

    调用代码如下

     1 require_once 'mysql_V1.class.php';
     2 require_once 'commonFun.php';
     3 $db = new mysql('localhost', 'root', '', "test");
     4 
     5 //select    查
     6 $db->select("user", "*", "username = 'system'");
     7 $result = $db->fetchArray(MYSQL_ASSOC);
     8 print_r($result);
     9 dump($db->printMessage());
    10 
    11 //insert    增
    12 //$userInfo = array('username'=>'system', 'password' => md5("system"));
    13 //$db->insert("user", $userInfo);
    14 //dump($db->printMessage());
    15 
    16 //update    改
    17 //$userInfo = array('password' => md5("123456"));
    18 //$db->update("user", $userInfo, "id = 2");
    19 //dump($db->printMessage());
    20 
    21 //delete    删
    22 //$db->delete("user", "id = 1");
    23 //dump($db->printMessage());
    24 
    25 //findAll   查询全部
    26 $db->findAll("user");
    27 $result = $db->fetchArray();
    28 dump($result);

    ps,个人比较喜欢tp的dump函数,所以在commonFun.php文件中拷贝了友好打印函数。使用时将其改为print_r()即可。

  • 相关阅读:
    poj3436(ACM Computer Factory)
    一位ACMer过来人的心得
    poj1459(Power Network)
    (转)网络流—最大流(Edmond-Karp算法)
    poj1611(The Suspects)
    构建之法阅读笔记01
    第三周总结
    全国疫情可视化地图
    第二周总结
    作业--数组(大数)
  • 原文地址:https://www.cnblogs.com/listenRain/p/mysql_V1.html
Copyright © 2011-2022 走看看