PHP新手用的Insert和Update语句构造类

前端之家收集整理的这篇文章主要介绍了PHP新手用的Insert和Update语句构造类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用方法
<div class="codetitle"><a style="CURSOR: pointer" data="44916" class="copybut" id="copybut44916" onclick="doCopy('code44916')"> 代码如下:

<div class="codebody" id="code44916">
$MysqL = new sqlstr("table1");
$MysqL->set("name","value");
$MysqL->set("name","1",true);
echo $MysqL->insertsql();

<div class="codetitle"><a style="CURSOR: pointer" data="36630" class="copybut" id="copybut36630" onclick="doCopy('code36630')"> 代码如下:
<div class="codebody" id="code36630">
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,strlen($keys)-1) ;}
if($values!=""){$values=substr($values,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,strlen($group)-1) ;}
return "update " . $this->tablename ." set $group where " . $cond;
}
}

原文链接:https://www.f2er.com/php/27251.html

猜你在找的PHP相关文章