PHP中使用Insert、Update語句的構造類

來源:互聯網
上載者:User
  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. * Insert、Update語句的構造類
  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. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.