珂珂的个人博客 - 一个程序猿的个人网站

mysqli 操作数据库帮助类

自己写的一个php  用mysqli操作数据库辅助类

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
<?php
if (stristr $_SERVER ["PHP_SELF"], basename __FILE__ ) )) {
    exit 'forbidden' );
}
require_once 'IDbHelper.php';
class MysqliHelper  implements IDbHelper {
    private $host;
    private $user;
    private $password;
    private $dbname;
    private $port;
    private $charset;
    private $mysqli;
    function __construct($host$user$password$dbname$port = 3306, $charset "utf-8") {
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->dbname = $dbname;
        $this->port = $port;
        $this->charset = $charset;
         
        $this->mysqli = new mysqli ( $host$user$password$dbname,$port );
        if ($this->mysqli->errno != 0) {
            echo "mysqli 连接错误:" $this->mysqli->error;
            exit ();
        }
    }
    function init() {
        if ($this->mysqli == null) {
            $this->mysqli = new mysqli ( $this->$host$this->$user$this->$password$this->$dbname,$this->port );
            if ($this->mysqli->errno != 0) {
                echo "mysqli 连接错误:" $this->mysqli->error;
                exit ();
            }
        }
    }
     
    /**
     * 返回查询结果集, 以 key 为键组织成关联数组, 每一个元素是一个对象.
     * 如果 key 为空, 则将结果组织成普通的数组.
     * $sql 必填 sql语句
     * $params 可选参数,以sql参数名作为key的数组
     */
    public function query($sql,$arr=null) {
        $this->init();
        $result $this->mysqli->query ( $sql );
        /*
         * $data = array(); while($row = $result->fetch_array(MYSQLI_ASSOC)){ $data[] = $row; } return $data;
         */
        echo $this->mysqli->error;
        return $result;
    }
     
    // 以数组的方式返回结果的第一条记录
    function getRow($sql,$arr=null) {
        $this->init();
        $result $this->mysqli->query ( $sql );
        echo $this->mysqli->error;
        while $row $result->fetch_array ( MYSQLI_ASSOC ) ) {
            return $row;
        }
         
        return null;
    }
     
    // 返回结果的第一条第一列记录
    function getObject($sql,$arr=null) {
        $this->init();
        $result $this->mysqli->query ( $sql );
        echo $this->mysqli->error;
        while $row $result->fetch_array ( MYSQLI_NUM ) ) {
            return $row [0];
        }
         
        return null;
    }
     
    // 执行sql语句
    function exec($sql,$arr=null) {
        $this->init();
        $count $this->mysqli->query ( $sql );
        echo $this->mysqli->error;
         
        return $this->mysqli->affected_rows;
    }
    public function data() {
        $this->init();
        return $this->mysqli;
    }
     
    function Insert($tableName$arr)
    {
        return 0;
    }
     
     
    function  Update($tableName,$primary,$arr)
    {
      return 0;
    }
     
    function __destruct() {
        if ($this->mysqli) {
            //$this->mysqli->close ();
            $this->mysqli = null;
        }
    }
}
 
?>



上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


0 评论

   5/8/2014 4:58:04 PM     58.248.2....

测试......


查看所有评论

给个评论吧