클래스 모델{
protected $tableName="";//表name称
protected $pOb;//pdo类对象
function __construct(){
$pdo=new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USERNAME,DB_PASSWORD);
$pdo->exec("세트 이름 ".DB_CHARSET);
$this->pOb=$pdo;
}
/*
* 작업:增
* 参数:array $arr exp:array('字段name'=>值,'字段name'=>值,....)
* return:int|false
*/
function add($arr){
//拼sql语句
$kArr=array_keys($arr);
$kStr=join(",",$kArr);
$vArr=array_values($arr);
$pStr = '';
foreach($vArr as $s=>$y){
$vname = "p".$s;
$pStr.=':'.$vname.',';
}
$pStr = substr($pStr,0,-1);
$sql = "{$this->tableName}($kStr) 값($pStr)에 삽입";
print_r($sql);
$pdoS = $this->pOb ->준비($sql);
foreach($vArr as $k=>$y){
$vname = "p".$k;
$$v이름 = $y;
var_dump($vname,$$vname);
$pdoS -> bindParam(":".$vname, $$vname,PDO::PARAM_STR);
}
$re = $pdoS -> 실행();
if($re){//添加成功
//返回主键id值
return $this->pOb->lastInsertId();
}
//返回值
return $re;
}
공개 함수 삭제($arrWhere){
if(!empty($arrWhere)){
$strW = " where ";
foreach($arrWhere as $kW=>$vW){
$kn = str_replace(":", "", $kW);
if(count($arrWhere)==1){
$strW .= $kn."=".$kW;
}else{
$strW .= $kn."=".$kW." 및 ";
}
}
if(count($arrWhere)>1){
$strW .= " 1=1 ";
}
}
$sql = "{$this->tableName}에서 삭제".$strW;
print_r($sql);
$pdoS = $this->pOb->준비($sql);
foreach($arrWhere as $kW=>$vW){
$kn = str_replace(":", "", $kW);
$$kn = $vW;
if(is_int($vW)){
$pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
}else if(is_float($vW)){
$pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
}else{
$pdoS->bindParam($kW,$$kn,PDO::PARAM_STR);
}
}
$re=$pdoS->execute();
if($re){
true를 반환합니다.
}else {
false를 반환합니다.
}
}
함수 업데이트($arrSet,$arrWhere){
//拼sql语句
$str = "";
$n=0;
foreach($arrSet as $kS=>$vS){
$str .= ",".$kS."=:p".$n++;
}
$str = substr($str, 1);
foreach($arrWhere as $kW=>$vW){
$kn=str_replace(":","",$kW);
if(count($arrWhere)==1){
$strW .= $kn."=".$kW;
}else{
$strW .= $kn."=".$kW." 및 ";
}
}
if(count($arrWhere)>1){
$strW .= " 1=1 ";
}
$sql="update {$this->tableName} set {$str} where ".$strW;
//print_r($sql);
$pdoS=$this->pOb->준비($sql);
$x = 0;
foreach($arrSet as $kS=>$vS){
$kS = ":p".$x++;
$$kS = $vS;
if(is_int($vS)){
$pdoS->bindParam($kS,$$kS,PDO::PARAM_INT);
}else if(is_float($vS)){
$pdoS->bindParam($kS,$$kS,PDO::PARAM_INT);
}else{
$pdoS->bindParam($kS,$$kS,PDO::PARAM_STR);
}
}
foreach($arrWhere as $kW=>$vW){
$kn=str_replace(":","",$kW);
$$kn=$vW;//$p0 $p1 $p2
if(is_int($vW)){
$pdoS->bindParam($kW,$$kn,PDO::PARAM_INT );
}else if(is_float($vW)){
$pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
}else{
$pdoS->bindParam($kW,$$kn,PDO::PARAM_STR);
}
}
$re=$pdoS->execute();
if($re){
true를 반환합니다.
}else{
false를 반환합니다.
}
}
//查
기능 선택($field="*",$ArrayWhere="",$order="",$limit=""){
if(!empty($ArrayWhere)){
$strW = " where ";
foreach($ArrayWhere as $kW=>$vW){
$kn=str_replace(":","",$kW);
if(count($ArrayWhere)==1){
$strW .= $kn."=".$kW;
}else{
$strW .= $kn."=".$kW." 및 ";
}
}
if(count($ArrayWhere)>1){
$strW .= " 1=1 ";
}
}
if(!empty($order)){
$order="order by ".$order;
}
if(!empty($limit)){
$limit="limit ".$limit;
}
// 表name에서 字段列表를 선택합니다. 여기서 条件 순서는 字段 desc|asc 제한 시작, 길이입니다.
$sql="{$this->tableName} {$strW} {$order} {$limit}에서 {$field} 선택";
//print_r($sql);
$pdoS=$this->pOb->준비($sql);
if(!empty($ArrayWhere)){
foreach($ArrayWhere as $kW=>$vW){
$kn=str_replace(":","",$kW);
$$kn=$vW;
if(is_int($vW)){
$pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
}else if(is_float($vW)){
$pdoS->bindParam($kW,$$kn,PDO::PARAM_INT);
}else{
$pdoS->bindParam($kW,$$kn,PDO::PARAM_STR);
}
}
}
$re=$pdoS->execute();
if($re){
$pdoS->setFetchMode(PDO::FETCH_ASSOC);
return $pdoS->fetchAll();
}else {
false를 반환합니다.
}
}
}