Home  >  Article  >  Backend Development  >  Constructed classes using Insert and Update statements in PHP

Constructed classes using Insert and Update statements in PHP

WBOY
WBOYOriginal
2016-07-25 08:57:43946browse
  1. $mysql = new sqlstr("table1");
  2. $mysql->set("name","value");
  3. $mysql->set("name","1",true);
  4. echo $mysql->insertSql();
复制代码

2,insert与update实现的php构造类

  1. /**
  2. * Construction class of Insert and Update statements
  3. * edit: bbs.it-home.org
  4. */
  5. class sqlstr
  6. {
  7. private $param=array();
  8. private $tablename;
  9. function sqlstr($tablename)
  10. {
  11. $this->tablename = $tablename;
  12. }
  13. public function set($name,$value,$isnum=false){
  14. $value = str_replace("'","''",$value);
  15. $this->param[$name]=array($value,$isnum);
  16. }
  17. public function insertSql(){
  18. $keys="";
  19. $values="";
  20. foreach($this->param as $key =>$value){
  21. $keys = $keys . $key . ",";
  22. if($value[1]){
  23. $values = $values . $value[0] . ",";
  24. }else{
  25. $values = $values . "'" . $value[0] . "',";
  26. }
  27. }
  28. if($keys!=""){$keys=substr($keys,0,strlen($keys)-1) ;}
  29. if($values!=""){$values=substr($values,0,strlen($values)-1) ;}
  30. return "insert into " .$this->tablename ."($keys) values($values)";
  31. }
  32. public function updateSql($cond){
  33. $group="";
  34. foreach($this->param as $key =>$value){
  35. $group .= $key . "=";
  36. if($value[1]){
  37. $group.= $value[0] . ",";
  38. }else{
  39. $group.= "'" . $value[0] . "',";
  40. }
  41. }
  42. if($group!=""){$group=substr($group,0,strlen($group)-1) ;}
  43. return "update " . $this->tablename ." set $group where " . $cond;
  44. }
  45. }
复制代码


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