-
-
클래스 모델{ - 보호됨 $tableName="";//테이블 이름
- 보호됨 $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('필드 이름'=>값,'필드 이름'=>값,...)
- * return:int|false
- */
- 함수 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);
tableName}($kStr) 값($pStr)에 삽입";
print_r($sql)
- $pdoS = $this->pOb ->prepare($sql)
- foreach($vArr as $k=>$y){
- $vname = "p".$k; vname = $y
- var_dump($vname,$$vname)
- $pdoS ->
}
- $re = $pdoS ->execute()
- if($re){//추가 성공
- //기본 키 ID 값 반환
- return $ this->pOb->lastInsertId();
- }
- //반환 값
- return $re
- }
- 공용 함수 delete($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 ->prepare($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){
- return true; else {
- return false;
- }
- }
- function update($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->prepare($sql)
- $x = 0
- foreach($arrSet as $ kS= >$vS){
$kS = ":p".$x
- $$kS = $vS;
- < ;p> ;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){
- return true;
}else{
- return false
- }
//확인 - function select($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->prepare($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를 반환합니다.
- }
- }
- }
-
复代码
|