ThinkPHP - 连贯操作 - 【实现机制】
2016-02-01 13:54
681 查看
<?php //模型类 class Model { //数据库连接 private $_conn = NULL; //where语句 private $_where = NULL; //表名称 private $_tableName = NULL; //构造方法,接收表名称 public function __construct($tabName){ //给属性赋值 $this->_tableName = $tabName; //连接数据库 $this->_conn = mysqli_connect('localhost', 'root', '12345678', 'test'); //设置字符集编码 mysqli_set_charset($this->_conn, 'set names utf8'); } //where方法 public function where($whe){ //判断是否为空值 if ( empty($whe) ) { $this->_where = NULL; } else { $this->_where = ' where ' . $whe; } //返回对象 return $this; } //select方法 public function select(){ //存储数据 $dataArr = array(); //构造sql语句 $sql = 'select * from tp_' . strtolower($this->_tableName) . $this->_where; //执行sql,获取句柄 $resHandle = mysqli_query($this->_conn, $sql); //返回结果集 while ( !!$res = mysqli_fetch_array($resHandle, MYSQLI_ASSOC )) { $dataArr[] = $res; } //返回数据 return $dataArr; } //其余方法,待补充...... } $user = new Model('User'); $result = $user->where('id > 10')->select(); print_r($result);
相关文章推荐
- window下面搭建php集成环境xampp
- php session 会话控制[转]
- php strtotime一个小功能需要注意
- Yii2 使用 RESTful 写API接口 实例
- PHP数组传给js
- ntp 配置详解(转载后整理汇总)
- PHP自带方法验证邮箱是否存在
- laravel安装
- PHP 采集大全
- FTP服务器架设(1)简介
- PHP学习(3)—在HTML中嵌入PHP
- PHP学习(3)—在HTML中嵌入PHP
- PHP和MySQL Web开发学习笔记介绍
- PHP和MySQL Web开发学习笔记介绍
- 关于XAMPP下使用PHPBB或者Discuz!
- Matplotlib 生成不同大小的subplots
- PHP乱码完美解决
- PHP中替换换行符方法总结
- php下保存远程图片到本地的函数
- yii2.0 继承一个和自己重名的类?