台州温岭做网站请找我们 (专业建站 | 诚信服务) 13958630945 在线客服 联系技术

佳源设计|台州网站建设|佳源网页设计

网站当前位置: 首页>>建站知识>>MySQL数据库操作类(PHP实现,支持连贯操作)
建站流程
 
建站知识
 
网站建站疑难
 
网站设计_图象处理
 
网站设计_Css样式
 
网站建设_Flash疑难
 
手机网站建设
 
 建站知识搜索
 

台州网站建设
 
网站优化
 
提交疑问
 建站知识 > MySQL数据库操作类(PHP实现,支持连贯操作)

MySQL数据库操作类(PHP实现,支持连贯操作)

3606次浏览
2016-12-22

使用过ThinkPHP框架的同学可能会对于其中数据库模型操作特别有好感,ThinkPHP提供了数据库操作的简单的操作,对于连接数据库,数据库的增删改查等数据操作都非常的nice,同时支持连贯操作,对于那些不习惯写sql语句的同学真是大大的便利。(注:sql还是很重要的,不要因为用了框架就把原先的忘了)。 
而在笔者使用PHP操作Redis实现后台任务的过程中,也想要借助这种便利,但无奈redis操作单独的类,直接访问其中的controller文件的话,总是会提示M方法失败,导致此模型方法不能使用。万般无奈之下,只能自己来实现一下了。 
借助PHP的mysqli相关函数,进行MySQL数据库操作类的实现。此程序中提供数据库的操作包括:数据库的连接,数据库的选择,数据库用户的选择,相应数据库中所有数据表名的查看;数据表的操作包括:相应的数据表中字段全部属性的查看,数据表的增删改查操作,数据表查询、插入的连贯操作等

<?php
/**
 * Author: helen
 * CreateTime: 2016/4/12 20:14
 * description: 数据库操作类(仅对接MySQL数据库,主要利用MySQLi函数)
 */
class Database{

    //MySQL主机地址
    private $_host;
    //MySQL用户名
    private $_user;
    //MySQL用户密码
    private $_password;
    //指定数据库名称
    private $_database;
    //MySQL数据库端口号
    private $_port;
    private $_socket;
    //当前数据库对象
    private $_dbObj;
    //数据库表
    private $_table;
    //数据库表对象
    private $_tableObj;
    // 最近错误信息
    protected $error            =   '';
    // 数据信息
    protected $data             =   array();
    // 查询表达式参数
    protected $options          =   array();
    protected $_validate        =   array();  // 自动验证定义
    protected $_auto            =   array();  // 自动完成定义
    protected $_map             =   array();  // 字段映射定义
    protected $_scope           =   array();  // 命名范围定义
    // 链操作方法列表
    protected $methods          =   array('strict','order','alias','having','group','lock','distinct','auto','filter','validate','result','token','index','force');

    /**
     * Database类初始化函数
     * 取得DB类的实例对象 字段检查
     * @access public
     * @param string $host MySQL数据库主机名
     * @param string $user MySQL数据库用户名
     * @param string $password MySQL数据库密码
     * @param string $database 指定操作的数据库
     * @return mixed  数据库连接信息、错误信息
     */
    public function __construct($host,$user,$passowrd,$database,$port=3306){
        $this->_initialize();
        if(!isset($host)||!isset($user)||!isset($passowrd)||!isset($database)){
            return false;
        }else{
            $this->_host     = $host;
            $this->_user     = $user;
            $this->_password = $passowrd;
            $this->_database = $database;
            $this->_port     = $port;
            $_dbObj = new mysqli($host,$user,$passowrd,$database,$port);
            if($_dbObj->connect_errno){
                $this->error = $_dbObj->connect_error;
                return false;
            }else{
                $this->_dbObj = $_dbObj;
                return $this;
            }
        }
    }
    /**
     * 错误信息函数
     * 返回数据库操作过程中最后一次执行时的错误信息
     * @access public
     * @return mixed  数据库连接错误信息(正常返回'')
     */
    public function error(){
        return $this->error;
    }
    // 回调方法 初始化模型
    protected function _initialize() {}
    /**
     * 设置数据对象的值
     * @access public
     * @param string $name 名称
     * @param mixed $value 值
     * @return void
     */
    public function __set($name,$value) {
        // 设置数据对象属性
        $this->data[$name] = $value;
    }

    /**
     * 获取数据对象的值
     * @access public
     * @param string $name 名称
     * @return mixed
     */
    public function __get($name) {
        return isset($this->data[$name])?$this->data[$name]:null;
    }

    /**
     * 检测数据对象的值
     * @access public
     * @param string $name 名称
     * @return boolean
     */
    public function __isset($name) {
        return isset($this->data[$name]);
    }

    /**
     * 销毁数据对象的值
     * @access public
     * @param string $name 名称
     * @return void
     */
    public function __unset($name) {
        unset($this->data[$name]);
    }
    /**
     * 利用__call方法实现一些特殊的方法(对于调用类中不存在方法的解决方案)
     * @access public
     * @param string $method 方法名称
     * @param array $args 调用参数
     * @return mixed
     */
    public function __call($method,$args) {
        /*if(in_array(strtolower($method),$this->methods,true)) {
            // 连贯操作的实现
            $this->options[strtolower($method)] =   $args[0];
            return $this;
        }elseif(in_array(strtolower($method),array('count','sum','min','max','avg'),true)){
            // 统计查询的实现
            $field =  isset($args[0])?$args[0]:'*';
            return ;
        }elseif(strtolower(substr($method,0,5))=='getby') {
            // 根据某个字段获取记录
            $field   =   parse_name(substr($method,5));
            $where[$field] =  $args[0];
            return ;
        }elseif(strtolower(substr($method,0,10))=='getfieldby') {
            // 根据某个字段获取记录的某个值
            $name   =   parse_name(substr($method,10));
            $where[$name] =$args[0];
            return ;
        }elseif(isset($this->_scope[$method])){// 命名范围的单独调用支持
            return ;
        }else{

        }*/
    }
    /*
     * 选择数据库
     * @access public
     * @param string $database 选择的数据库名称
     * @return mixed 数据库连接信息
     * */
    public function select_db($database){
        $select_db = mysqli_select_db($this->_dbObj,$database);
        if($select_db){
            $this->_database = $database;
            $_dbObj = new mysqli($this->_host,$this->_user,$this->_password,$database,$this->_port);
            $this->_dbObj = $_dbObj;
            return $this;
        }else{
            $this->error = mysqli_error($this->_dbObj);
            return false;
        }
    }
    /*
     * 数据库用户更换
     * @access public
     * @param string $user 数据库用户名称
     * @param string $password 数据库用户密码
     * @return mixed 数据库连接信息
     * */
    public function change_user($user,$password){
        $change_user = mysqli_change_user($this->_dbObj,$user,$password,$this->_database);
        if($change_user){
            $this->_user = $user;
            $this->_password = $password;
            $_dbObj = new mysqli($this->_host,$this->_user,$this->_password,$this->_database,$this->_port);
            $this->_dbObj = $_dbObj;
            return $this;
        }else{
            $this->error = mysqli_error($this->_dbObj);
            return false;
        }
    }
    /*
     * 查询数据库中所有的表名
     * @access public
     * @return array 数据表的数量和表名
     * */
    public function tables(){
        $sql = 'show tables';
        $search_res = mysqli_query($this->_dbObj,$sql);
        if($search_res){
            $num_rows = $search_res->num_rows;
            $tables_msg = array(
                'count'=>$num_rows,
                'tables'=>array()
            );
            for($i=0;$i<$num_rows;$i++){
                $row = $search_res->fetch_assoc();
                $key = 'Tables_in_'.$this->_database;
                array_push($tables_msg['tables'],$row[$key]);
            }
            mysqli_free_result($search_res);
            return $tables_msg;
        }else{
            mysqli_free_result($search_res);
            return false;
        }
    }
    /*
     * 获取指定表中所有信息
     * @access public
     * @param string $table 数据表名称
     * @return array 数据表的详细信息
     * */
    public function select_table($table){
        $sql = 'select * from '.$table;
        $search_res = mysqli_query($this->_dbObj,$sql);
        if($search_res){
            $this->_table = $table;
            $table_msg = self::query_handle($search_res);
            $this->_tableObj = $table_msg;
            mysqli_free_result($search_res);
            return $table_msg;
        }else{
            mysqli_free_result($search_res);
            return false;
        }
    }
    /*
     * 获取指定表的字段详细信息
     * @access public
     * @param string $table 数据表名称
     * @return array 数据表的字段详细信息
     * */
    public function select_table_fields($table){
        $sql = 'show fields from '.$table;
        $search_res = mysqli_query($this->_dbObj,$sql);
        if($search_res){
            $this->_table = $table;
            $fields_msg = self::query_handle($search_res);
            mysqli_free_result($search_res);
            return $fields_msg;
        }else{
            mysqli_free_result($search_res);
            return false;
        }
    }
    /*
     * 获取数据表中指定字段信息(允许多字段同时查询)
     * @access public
     * @param mixed $field 指定字段(字符串传入使用,间隔)
     * @return array 数据表中指定字段信息
     * */
    public function getField($field){
        $fields = self::param_handle($field);
        $count = count($fields);
        for($i=0;$i<$count;$i++){
            $index = $fields[$i];
            $sql = 'select '.$index.' from '.$this->_table;
            $res = mysqli_query($this->_dbObj,$sql);
            $field_msg[$index] = self::query_handle($res);
        }
        return $field_msg;
    }
    /*
     * mysqli_query函数结果处理函数
     * @access protected
     * @param object $obj mysqli_query函数结果
     * @return array 数据表中指定字段信息
     * */
    protected function query_handle($obj){
        $res = array();
        for($i=0;$i<$obj->num_rows;$i++){
            $row = $obj->fetch_assoc();
            array_push($res,$row);
        }
        return $res;
    }
    /*
     * 传入参数处理函数
     * @access protected
     * @param mixed $param 传入参数
     * @return array 处理后数组数据
     * */
    public function param_handle($param){
        if(is_string($param)&&!empty($param)){
            $params = explode(',',$param);
        }elseif(is_array($param)&&!empty($param)){
            $params = $param;
        }else{
            return false;
        }
        return $params;
    }
    /*
     * 查询表达式参数处理函数
     * @access protected
     * @param mixed $param 传入参数(where limit order)
     * @return string 处理后字符串数据
     * */
    public function options_handle($param){
        if(is_numeric($param)){
            $option = $param;
        }elseif(is_string($param)&&!empty($param)&&!is_numeric($param)){
            $params = explode(',',$param);
            $count = count($params);
            $option = implode(' and ',$params);
        }elseif(is_array($param)&&!empty($param)){
            $params = $param;
            $count = count($params);
            $arr = array();
            foreach($param as $key=>$value){
                $tip = "$key=$value ";
                array_push($arr,$tip);
            }
            $option = implode(' and ',$arr);
        }else{
            return false;
        }
        return $option;
    }
    /*
     * 查询表达式$options处理函数
     * @access protected
     * @return string 处理后字符串数据
     * */
    protected function option(){
        $options = $this->options;
        $option = '';
        if(isset($options['where'])){
            $option .= 'where '.$options['where'].' ';
        }
        if(isset($options['order'])){
            $option .= 'order by '.$options['order'].' '.$options['order_type'].' ';
        }
        if(isset($options['limit'])){
            $option .= 'limit '.$options['limit'];
        }
        return $option;
    }
    /*
     * 根据查询表达式查询数据(符合条件的所有记录)
     * @access public
     * @return array 满足查询表达式的特定数据
     * */
    public function find(){
        $option = self::option();
        $sql = 'select * from '.$this->_table.' '.$option;
        $search_res = mysqli_query($this->_dbObj,$sql);
        $msg = self::query_handle($search_res);
        return $msg;
    }
    /*
     * 查询表达式 where处理函数
     * @access public
     * @param mixed $where where查询条件
     * @return object $this
     * */
    public function where($where){
        $this->options['where'] = self::options_handle($where);
        return $this;
    }
    /*
     * 查询表达式 limit处理函数
     * @access public
     * @param mixed $limit limit查询条件(数字)
     * @return object $this
     * */
    public function limit($limit){
        $this->options['limit'] = self::options_handle($limit);
        return $this;
    }
    /*
     * 查询表达式 order处理函数
     * @access public
     * @param string $order order查询条件
     * @param string $type order查询条件的顺序(默认降序)
     * @return object $this
     * */
    public function order($order,$type='desc'){
        $this->options['order'] = $order;
        $this->options['order_type'] = $type;
        return $this;
    }
    /*
     * 数据处理函数(最多处理二维数据)
     * @access public
     * @param array $data 需要插入的数据
     * @return object $this
     * */
    public function data(array $data){
        $values = array();
        $fields = array();
        if(is_array($data)){
            foreach($data as $key=>$value){
                if(is_array($value)){       //二维数组
                    $tip = 1;
                    array_push($values,'('.implode(',',array_values($value)).')');
                    array_push($fields,'('.implode(',',array_keys($value)).')');
                }else{      //一维数组
                    $tip = 0;
                }
            }
        }else{
            return false;
        }
        if(!$tip){
            array_push($values,'('.implode(',',array_values($data)).')');
            array_push($fields,'('.implode(',',array_keys($data)).')');
        }
        $this->data['fields'] = $fields[0];
        $this->data['values'] = implode(',',$values);
        return $this;
    }
    /*
     * 数据新增函数
     * @access public
     * @return mixed 数据库新增信息
     * */
    public function add(){
        $fields = $this->data['fields'];
        $values = $this->data['values'];
        $sql = 'INSERT INTO '.$this->_table.$fields.'VALUES'.$values;
        $res = mysqli_query($this->_dbObj,$sql);
        return $res;
    }
    /*
     * 数据更新函数(一维数组)
     * @access public
     * @param array $data 需要更新的数据
     * @return mixed 数据库新增信息
     * */
    function save(array $data){
        $tip = array();
        if(is_array($data)){
            foreach($data as $key=>$value){
                array_push($tip,"$key=$value");
            }
        }else{
            return false;
        }
        $set_msg = implode(',',$tip);
        $sql = 'UPDATE '.$this->_table.' SET '.$set_msg.' WHERE '.$this->options['where'];
        $res = mysqli_query($this->_dbObj,$sql);
        return $res;
    }
    /*
     * 数据删除函数
     * @access public
     * @return mixed 数据库删除信息
     * */
    public function delete(){
        $sql = 'DELETE FROM '.$this->_table.' WHERE '.$this->options['where'];
        $res = mysqli_query($this->_dbObj,$sql);
        return $res;
    }
    /*
     * SQL语句查询
     * */
    public function query($sql){
        $search_res = mysqli_query($this->_dbObj,$sql);
        return $search_res;
    }
    /*
     * mysql中查询语句
     * */
    protected function sql(){
        /*
         * 基本SQL语句
         * 插入数据:INSERT INTO tb_name(id,name,score)VALUES(NULL,'张三',140),(NULL,'张四',178),(NULL,'张五',134);
         * 更新语句:UPDATE tb_name SET score=189 WHERE id=2;
         * 删除数据:DELETE FROM tb_name WHERE id=3;
         * WHERE语句:SELECT * FROM tb_name WHERE id=3;
         * HAVING 语句:SELECT * FROM tb_name GROUP BY score HAVING count(*)>2
         * 相关条件控制符:=、>、<、<>、IN(1,2,3......)、BETWEEN a AND b、NOT AND 、OR Linke()用法中      %  为匹配任意、  _  匹配一个字符(可以是汉字)IS NULL 空值检测
         * MySQL的正则表达式:SELECT * FROM tb_name WHERE name REGEXP '^[A-D]'   //找出以A-D 为开头的name
         * */
    }
    /*
     * 关闭连接
     * */
    public function close(){
        $close = mysqli_close($this->_dbObj);
        if($close){
            return true;
        }else{
            return false;
        }
    }
    function __destruct(){
        mysqli_close($this->_dbObj);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501

操作示例: 
首先实例化此类,其中需要输入host(数据库地址)、user(数据库用户)、password(数据库用户密码)、database(数据库名称)此四种信息,进行数据库的连接,然后即可调用select_table()方法,其中传入数据表名,从而设置对指定表的操作,从而即可利用连贯操作进行相应数据的增删改查。 其中参数基本均支持字符串和数组两种形式。

include '/classes/db.php';
            $db = new \Database('localhost', 'root', '901230', 'weixin');
            //$db = new \mysqli('localhost','root','901230','weixin');
            //$db->select_db('visitor');
            //dump($db->error());
            //$db->change_user('helen','901230');
            $table = 'zyd_fuweng_user';
            //dump($db->select_table_fields($table));
            //dump($db->error());
            $db->select_table($table);
            $param1 = '123';
            $param2 = 'id>1,record>100';
            $param3 = array(
                /*array('count'=>1,'openid'=>'123','record'=>'100'),
                array('count'=>2,'openid'=>'234','record'=>'200'),*/
                array('count' => 4, 'openid' => '456', 'record' => '500')
            );
            $param4 = array('count' => 4, 'openid' => '456', 'record' => '500');
            //dump($db->where('id=4')->save($param4));
            dump($db->where('count=4')->delete());

            die;
            dump($db->data($param3)->add());
            dump(array_keys($param3));
            dump(array_values($param3));
            dump(implode(',', array_values($param3)));
            dump(implode(',', array_keys($param3)));

            dump($db->where($param2)->order('id')->limit(2)->find());
            dump($db->options_handle($param1));
            dump($db->options_handle($param2));
            dump($db->options_handle($param3));
            $array = array('id', 'count');

            $num = '123';
            if (is_string($num)) {
                echo 'true';
            }

            dump($db->getField($array));
            dump($db->select_table_fields($table));
            /*$array = array('a','b');
            $array1 = array();
            dump($db->getField('a,b'));
            dump($db->getField($array));
            dump($db->getField($array1));
            dump($db->getField(''));*/
            /*$str = '';
            if(empty($str)){
                echo 'true';
            }*/
            /*$model = M('zyd_fuweng_user');
            $res = $model->getField('create_time,count');
            dump($res);*/
            die;
            //
            $table = 'zyd_fuweng_user';
            //选择指定的数据库,并返回其中全部信息
            $table_msg = $db->select_table($table);
            //选择指定数据库,返回数据库的字段信息
            $table_field_msg = $db->select_table_fields($table);
            //条件搜索,传入条件均为数据
            $where = array(
                'id' => 1
            );
            $data = array(
                'headimgurl' => 'helen.jpg'
            );
            dump($db->where($where)->field('field'));
            dump($table);


            /*$dbObj = new \mysqli('localhost','helen','901230','weixin','3306');
            //$query = 'select * from zyd_fuweng_user';
            $query = 'show fields from zyd_fuweng_user';
            $tables = mysqli_query($dbObj,$query);
            //dump($tables);
            $count = $tables->num_rows;
            $arr = array();
            for($i=0;$i<$count;$i++){
                $row = $tables->fetch_assoc();
                //dump($row);
                array_push($arr,$row);
            }
            mysqli_free_result($tables);
            dump($arr);
            die;
            $query1 = 'select * from zyd_fuweng_user';
            $table_msg = mysqli_query($dbObj,$query1);
            //输出查询结果
            $num = $table_msg->num_rows;
            for($i=0;$i<$num;$i++){
                $row = $table_msg->fetch_assoc();
                dump($row);
            }
            dump($dbObj);
            dump($tables);
            dump($table_msg);
            $res = mysqli_close($dbObj);
            dump($res);*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

页面所在本站地址: http://www.52-life.net/Nshow_reurl.asp?reurl=MySQL_database
上一篇 >>No News
下一篇 >>ThinkPHP3.2完全开发手册