2020年6月5日
<?php
echo"<pre>";
include './demo6.php';
include './Model.demo7.php';
$obj=Model::connect();
$res=$obj
->table('books')
->where('book_id','=',1000)
->sava([
'title' => 'qt'
]);
var_dump("执行语句结果:",$res);
var_dump($obj);
echo"</pre>";
class Model{
private static $obj =null;//保存model对象
protected $link= null;//保存数据库链接
protected $tableName;//保存表名
protected $sql;//保存完成的sql语句
protected $where ='WHERE';//保存条件
private function{
try{
//连接数据库
$this->link=new PDO(DSN,USER,PASS);
//设置错误报告
$this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
exit($e->getMessage());
}
}
public static function connect(){
if(self::$obj===null){
self::$obj=new self();
}
return self::$obj;
}
public static add($data){
$keys=array_keys($data);
$vals=array_values($data);
//生成字段
$keys_str=implode(',',$keys);
$vals_str=sprintf("'$s'",implode("','",$vals));
$this->getsql('add',[
$keys_str,
$vals_str,
]);
return $this->exec();
}
//数据库删除函数
public function delete{
$this->getsql('del');
return $this->exec();
}
//修改函数
public function sava{
$sava_data=[];
foreach($data as $k => $v){
$sava_data[]=sprintf("%s='$s'",$k,$v);
}
$sava_data_str=imlode(',',$sava_data);
$this->getsql('edit',[
$sava_data_str
]);
return $this->exec();
}
//辅助函数 设置表名
public function table($name){
$this->tableName=$name;
return $this;
}
protected function getsql($type,$data=[]){
if(!$this->tableName){
exit("请设置表名");
}
switch($type){
case 'add':
$this->sql = sprintf("INSERT INTO %s(%s) VALUES(%s)",$this->tableName,$data[0],$data[1]);
break;
case 'del':
if($this->where == 'WHERE'){
exit("delete操作不能没有where条件;");
}
$this->sql=sprintf("DELETE FROM %s %s",$this->tableName,$this->where);
break;
case 'edit':
if($this->where == 'WHERE'){
$this->sql=sprintf("UPDATE %s SET %s %s",$this->tableName,$data[0],$this->where);
break;
}
}
}
public function exec(){
return $this->link->exec($this->sql);
}
public function where($field,$mark,$data,$rel='AND'){
if($this->where=='WHERE'){
//第一个条件
$this->.=sprintf("%s %s '%s'",$field,$mark,$data);
}else{
//第二个条件和第n个
$this->.=sprintf(" %s %s %s %s",$rel,$field,$mark,$data);
}
return $this;
}
//设置查询时的字段
public function field(){
$p=func_get_args();
$this->field-implode(',',$p);
return $this;
}
}
<?php
echo"<pre>";
include './demo6.php';
include './Model.demo7.php';
$obj=Model::connect();
$res=$obj
->table('books')
->where('book_id','=',1000)
->sava([
'title' => 'qt'
]);
var_dump("执行语句结果:",$res);
var_dump($obj);
echo"</pre>";
class Model{
private static $obj =null;//保存model对象
protected $link= null;//保存数据库链接
protected $tableName;//保存表名
protected $sql;//保存完成的sql语句
protected $where ='WHERE';//保存条件
private function{
try{
//连接数据库
$this->link=new PDO(DSN,USER,PASS);
//设置错误报告
$this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
exit($e->getMessage());
}
}
public static function connect(){
if(self::$obj===null){
self::$obj=new self();
}
return self::$obj;
}
public static add($data){
$keys=array_keys($data);
$vals=array_values($data);
//生成字段
$keys_str=implode(',',$keys);
$vals_str=sprintf("'$s'",implode("','",$vals));
$this->getsql('add',[
$keys_str,
$vals_str,
]);
return $this->exec();
}
//数据库删除函数
public function delete{
$this->getsql('del');
return $this->exec();
}
//修改函数
public function sava{
$sava_data=[];
foreach($data as $k => $v){
$sava_data[]=sprintf("%s='$s'",$k,$v);
}
$sava_data_str=imlode(',',$sava_data);
$this->getsql('edit',[
$sava_data_str
]);
return $this->exec();
}
//辅助函数 设置表名
public function table($name){
$this->tableName=$name;
return $this;
}
protected function getsql($type,$data=[]){
if(!$this->tableName){
exit("请设置表名");
}
switch($type){
case 'add':
$this->sql = sprintf("INSERT INTO %s(%s) VALUES(%s)",$this->tableName,$data[0],$data[1]);
break;
case 'del':
if($this->where == 'WHERE'){
exit("delete操作不能没有where条件;");
}
$this->sql=sprintf("DELETE FROM %s %s",$this->tableName,$this->where);
break;
case 'edit':
if($this->where == 'WHERE'){
$this->sql=sprintf("UPDATE %s SET %s %s",$this->tableName,$data[0],$this->where);
break;
}
}
}
public function exec(){
return $this->link->exec($this->sql);
}
public function where($field,$mark,$data,$rel='AND'){
if($this->where=='WHERE'){
//第一个条件
$this->.=sprintf("%s %s '%s'",$field,$mark,$data);
}else{
//第二个条件和第n个
$this->.=sprintf(" %s %s %s %s",$rel,$field,$mark,$data);
}
return $this;
}
//设置查询时的字段
public function field(){
$p=func_get_args();
$this->field-implode(',',$p);
return $this;
}
}
<?php
echo"<pre>";
include './demo6.php';
include './Model.demo7.php';
$obj=Model::connect();
$res=$obj
->table('books')
->where('book_id','=',1000)
->sava([
'title' => 'qt'
]);
var_dump("执行语句结果:",$res);
var_dump($obj);
echo"</pre>";
class Model{
private static $obj =null;//保存model对象
protected $link= null;//保存数据库链接
protected $tableName;//保存表名
protected $sql;//保存完成的sql语句
protected $where ='WHERE';//保存条件
private function{
try{
//连接数据库
$this->link=new PDO(DSN,USER,PASS);
//设置错误报告
$this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
exit($e->getMessage());
}
}
public static function connect(){
if(self::$obj===null){
self::$obj=new self();
}
return self::$obj;
}
public static add($data){
$keys=array_keys($data);
$vals=array_values($data);
//生成字段
$keys_str=implode(',',$keys);
$vals_str=sprintf("'$s'",implode("','",$vals));
$this->getsql('add',[
$keys_str,
$vals_str,
]);
return $this->exec();
}
//数据库删除函数
public function delete{
$this->getsql('del');
return $this->exec();
}
//修改函数
public function sava{
$sava_data=[];
foreach($data as $k => $v){
$sava_data[]=sprintf("%s='$s'",$k,$v);
}
$sava_data_str=imlode(',',$sava_data);
$this->getsql('edit',[
$sava_data_str
]);
return $this->exec();
}
//辅助函数 设置表名
public function table($name){
$this->tableName=$name;
return $this;
}
protected function getsql($type,$data=[]){
if(!$this->tableName){
exit("请设置表名");
}
switch($type){
case 'add':
$this->sql = sprintf("INSERT INTO %s(%s) VALUES(%s)",$this->tableName,$data[0],$data[1]);
break;
case 'del':
if($this->where == 'WHERE'){
exit("delete操作不能没有where条件;");
}
$this->sql=sprintf("DELETE FROM %s %s",$this->tableName,$this->where);
break;
case 'edit':
if($this->where == 'WHERE'){
$this->sql=sprintf("UPDATE %s SET %s %s",$this->tableName,$data[0],$this->where);
break;
}
}
}
public function exec(){
return $this->link->exec($this->sql);
}
public function where($field,$mark,$data,$rel='AND'){
if($this->where=='WHERE'){
//第一个条件
$this->.=sprintf("%s %s '%s'",$field,$mark,$data);
}else{
//第二个条件和第n个
$this->.=sprintf(" %s %s %s %s",$rel,$field,$mark,$data);
}
return $this;
}
//设置查询时的字段
public function field(){
$p=func_get_args();
$this->field-implode(',',$p);
return $this;
}
}
笔记:
Model:封装数据库的增删改查操作函数
数据库的新增操作:
INSERT INTO money(name,rmb)VALUES(‘QT’,’1000’)
INSERT INTO 表名(字段名,字段名2…)VALUES(值,值2…)
数组方式:[‘name’=>’qt’]按前面的格式;设置字段和值
数组的下标为字段名,数组的值为要添加的值
封装生成语句函数:
$type:add
del
edit
get
修改函数:
UPDATA money SET 字段=值,字段=值 WHERE id=1
封装where条件设置:
WHERE id= 1;
WHERE id= 1 OR id = 2;
WHERE id= 1AND name =2;
WHERE id= 1AND name =2 AND title = 3;
WHERE id= 1AND name =2 OR title = 3;
每调用一次则设置一次where条件;当第一次设置时则直接设置,当第二次或多次时那新增条件需要和原有条件相结合