PHP新手用的Insert和Update语句构造类_php教程-查字典教程网 set("name","value");$mysql->set("name","1",true);ech..."/>
PHP新手用的Insert和Update语句构造类
PHP新手用的Insert和Update语句构造类
发布时间:2016-12-29 来源:查字典编辑
摘要:使用方法复制代码代码如下:$mysql=newsqlstr("table1");$mysql->set("name","value");$m...

使用方法

复制代码 代码如下:

$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教程学习
    热门php教程学习
    编程开发子分类