您的位置:首页 > 编程语言 > PHP开发

通过一个简单的数据库操作类了解PHP链式操作的实现

2015-11-02 20:11 721 查看
class Model{
public $table; //操作的表;
private $opt; //查询的参数;
private $pri; //表的主键;
private $lastSql; //最后一条sql语句;
private $error;

public function __construct($table = ''){
$this->table = $table;

$db = @mysql_connect('127.0.0.1' , 'root' , 'admin'); //这里应该通过配置读取进来
if (!$db)
exit('抱歉!数据库链接失败!');
mysql_select_db('tpplh') or exit('对不起,没有这个数据库'); //数据库的选择

mysql_query("SET NAMES utf8"); //设定数据库字符集;
$this->getPrimary($table); //获取主键;
$this->opt(); //初始化查询参数;
}

public function getLastSql(){
return $this->lastSql;
}

public function getError(){
return $this->error();
}

#获取表的主键
private function getPrimary($table){
$this->lastSql = ' DESC ' . $table;
$result = $this->query($this->lastSql);
foreach ($result as $v) {
if ($v['Key'] == 'PRI') {
$this->pri = $v['Field'];
return;
}
}
}

#初始化查询参数
private function opt(){
$this->opt = array('table' => $this->table , 'pri' => $this->pri , 'where' => '' , 'orderby' => '' , 'having' => '' , 'orderway' => '' , 'group' => '' , 'limit' => '' , 'fields' => '*');
}

#临时表切换,不用另外在实例化类了;
public function table($table){
$this->opt['table'] = $table;
return $this; //返回切换后的对象;
}

public function save($data = null){
return $this->update($data);
}

#update更改
public function update($data = null){
if (is_null($data))
$data = $_POST;
if (!is_array($data))
return false;
if (isset($data[$this->pri])) { //如果里面有主键;就让where条件临时变为主键
$this->opt['where'] = ' WHERE ' . $this->pri . '=' . $data[$this->pri];
}
if (!$this->opt['where']) {
echo '增删改操作,必须要带where条件!';
exit;
}
$set = ''; //set字符串容器;
foreach ($data as $n => $v) {
$set .= $n . "='" . $v . "',";
}
$set = rtrim($set , ','); //去掉后面多余的逗号;
$this->lastSql = "UPDATE " . $this->opt['table'] . ' SET ' . $set . $this->opt['where'];
return $this->exe($this->lastSql);
}

#delete删除:delete from 表名 where 条件;
public function delete(){
$where = $this->opt['where'];
if (!$where) {
echo 'delete必须设置条件';
exit;
}
$this->lastSql = "DELETE FROM {$this->opt['table']} $where";
return $this->exe($this->lastSql);
}

#insert条件
public function insert($data = null){
if (is_null($data))
$data = $_POST;
if (!is_array($data))
return false;
$field = $value = "";
$field = implode("," , array_keys($data));
foreach ($data as $name => $v) {
$value .= "'" . addslashes($v) . "',";
}
$value = substr($value , 0 , -1);
$this->lastSql = "INSERT INTO " . $this->opt['table'] . "($field) VALUES($value)";
return $this->exe($this->lastSql);
}

public function add($data = null){
return $this->insert($data);
}

#limit 语句:
public function limit($arg){
$this->opt['limit'] = " LIMIT $arg";
return $this;
}

#order 语句:
public function order($arg){
$this->opt['orderby'] = " ORDER BY $arg";
return $this;
}

#orderway 语句:
public function orderway($arg){
$this->opt['orderway'] = " $arg";
return $this;
}

#group 语句:
public function group($arg){
$this->opt['group'] = " GROUP BY $arg";
return $this;
}

#where 条件
public function where($arg){
$this->opt['where'] = " WHERE $arg";
return $this;
}

#having 语句
public function having($arg){
$this->opt['where'] = " HAVING $arg";
return $this;
}

#fields 语句
public function field($arg){
$this->opt['fields'] = " $arg";
return $this;
}

#count 语句 统计数量
public function count(){
$this->lastSql = "select count(*) as t from " . $this->opt['table'] . $this->opt['where'];
$count = $this->query($this->lastSql);
return $count[0]['t'];
}

#select 语句
public function select(){
$this->lastSql = "SELECT {$this->opt['fields']} FROM {$this->opt['table']}{$this->opt['where']}{$this->opt['orderby']}{$this->opt['orderway']}{$this->opt['group']}{$this->opt['having']}{$this->opt['limit']}";
//echo $this -> lastSql;die;
$this->opt(); //查询完执行opt方法重新初始化参数;
return $this->query($this->lastSql);
}

#find 通过主键来查询
public function find($arg = null){
if ($this->pri and $arg) {
$this->opt['where'] = " WHERE {$this->pri}=$arg";
} else {
$this->opt['limit'] = " LIMIT 1";
}
return $this->select();
}

#发送增,删,改sql,成功返回最后一条的id或受影响条数;
public function exe($sql){
$this->lastSql = $sql;
if (mysql_query($sql)) {
$result = mysql_insert_id() ? mysql_insert_id() : mysql_affected_rows(); //如果该表有一个自增的id,就返回自增的id,如果没有就返回受影响的条数;
return $result;
}
}

#发送查询的sql语句,返回查询结果数组;
public function query($sql){
$this->lastSql = $sql;
$result = mysql_query($sql);
if (!$result)
return $this->error();
$rows = array(); //用于存储查询的数据;
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}

private function error(){
return $this->error = mysql_error(); //输出mysql产生的错误信息;
}
}
?>

总结如下:

链式部分就是对类中属性的进行更改,属性可以是数组

在链式部分最后返回的都是
$this


在最后输出方法中获取链式更改掉的属性进行操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: