Home  >  Article  >  Backend Development  >  Insert and Update statement construction classes for PHP novices_PHP tutorial

Insert and Update statement construction classes for PHP novices_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:03675browse

使用方法

复制代码 代码如下:

$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;
}
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/325180.htmlTechArticle使用方法 复制代码 代码如下: $mysql = new sqlstr("table1"); $mysql-set("name","value"); $mysql-set("name","1",true); echo $mysql-insertSql(); 复制代码 代码如下: c...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn