首页 > 资讯列表 > 编程/数据库 >> PHP

PHP 实现的Insert和Update语句构造类

PHP 2014-12-16 14:39:07 转载来源: 网络整理/侵权必删

本文是一个PHP 实现的Insert和Update语句构造类,感兴趣的同学参考下. 使用方法 $mysql = new sqlstr("table1"); $mysql->set("name","value"); $mysql->set("name","1",true); echo $mysql->insertSql(); class sqlstr { private $param=array(); private $tablename; function sqlstr($tablename) { $this->tablename = $tablename; } public function set($name,$value,$isnum=false){ $value = str_replace("'","''",$value); $this-

本文是一个PHP 实现InsertUpdate语句构造类,感兴趣的同学参考下.


使用方法


$mysql = new sqlstr("table1");
$mysql->set("name","value");
$mysql->set("name","1",true);
echo $mysql->insertSql();

class sqlstr
{
private $param=array();
private $tablename;
function sqlstr($tablename)
{
$this->tablename = $tablename;
}
public function set($name,$value,$isnum=false){
$value = str_replace("'","''",$value);
$this->param[$name]=array($value,$isnum);
}
public function insertSql(){
$keys="";
$values="";
foreach($this->param as $key =>$value){
$keys = $keys . $key . ",";
if($value[1]){
$values = $values . $value[0] . ",";
}else{
$values = $values . "'" . $value[0] . "',";
}
}
if($keys!=""){$keys=substr($keys,0,strlen($keys)-1) ;}
if($values!=""){$values=substr($values,0,strlen($values)-1) ;}
return "insert into " .$this->tablename ."($keys) values($values)";
}
public function updateSql($cond){
$group="";
foreach($this->param as $key =>$value){
$group .= $key . "=";
if($value[1]){
$group.= $value[0] . ",";
}else{
$group.= "'" . $value[0] . "',";
}
}
if($group!=""){$group=substr($group,0,strlen($group)-1) ;}
return "update " . $this->tablename ." set $group where " . $cond;
}
}

 


标签: PHP 实现 Insert Update 语句 构造


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持