PHP中使用Insert、Update语句的构造类
-
原创
-
2016-07-25 08:57:43
699浏览
- $mysql = new sqlstr("table1");
- $mysql->set("name","value");
- $mysql->set("name","1",true);
- echo $mysql->insertSql();
复制代码2,insert与update实现的php构造类
- /**
- * Insert、Update语句的构造类
- * edit: bbs.it-home.org
- */
- 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;
- }
- }
复制代码 |
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。