程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP新手用的Insert和Update語句構造類

PHP新手用的Insert和Update語句構造類

編輯:關於PHP編程

使用方法
復制代碼 代碼如下:
$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;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved