<?php
header("content-type:text/html;charset=utf-8");php
class Singleton{
//私有的靜態屬性
private static $dan="";
private static $obj="";
public $mysql="";
public $psd='root';
//私有的構造方法
private function __construct($mysql){
if(self::$obj==""){
self::$obj= new PDO($mysql,'root',$this->psd);
}
}
//私有的克隆方法
private function __clone(){
}
//公共的靜態方法
public static function instance($mysql="mysql:host=127.0.0.1;dbname=month7"){
// echo $mysql;die;
//判斷靜態變量是否爲空
if(empty(self::$dan)){
//實例化 賦值
self::$dan=new self($mysql);
return self::$dan;
}else{
return self::$dan;
}
}
//添加
public function insert($table,$data){
$kk="";
foreach($data as $k=>$v){
$kk.=$k.'='.'?'.',';
}
$arr=array();
//關聯數組變索引數組
foreach($data as $v){
$arr[]=$v;
}
// print_r($arr);die;
$kkk=rtrim($kk,',');
$ob = self::instance(); //調用靜態的方法
// $ob=self::$obj;
// print_r($ob);die;
$sth = $ob::$obj ->prepare("insert into $table SET $kkk "); //預處理
$ob::$obj->query("SET NAMES utf8");
// $sth = $ob ->prepare("insert into $table SET $kkk "); //預處理
$sth->execute($arr); //執行
// print_r($sth);
$lastid=$ob::$obj ->lastInsertId(); //返回新添加的id
return $lastid;
// return $sth->rowCount();html
}
//刪除
public function delete($id){
$ob=self::$obj;
// print_r(array($id));die;
$sth = $ob->prepare('delete from gather where id=?');
$sth->execute(array($id));
echo $sth->rowCount(); //返回的是1
}
//批刪
public function deletes($data){
$ob=self::$obj;
$clause = str_repeat("?,", count($data)-1) . "?";
$sql = "DELETE FROM gather WHERE id IN ($clause)";
$st =$ob->prepare($sql);
$st->execute($data);
echo $st->rowCount();
}
//修改
public function upload($data,$id,$field){
// echo $field;die;
// print_r($data);die;
$kk="";
foreach($data as $k=>$v){
$kk.=$k.'='.'?'.',';
}
$kkk=rtrim($kk,',');
$arr=array();
//關聯數組變索引數組
foreach($data as $v){
$arr[]=$v;
}
$arr[]=$id;
$ob=self::$obj;
self::$obj->query("SET NAMES utf8"); //設置字符集
$sth = $ob->prepare("update gather set $kkk where $field= ?");
$sth->execute($arr);
echo $sth->rowCount();
}
//條件查詢
public function select_one($table,$condition,$link){ //表名,條件,數據
$sth = self::$obj->prepare("SELECT * FROM $table WHERE $condition = ?"); //預處理
self::$obj->query("SET NAMES utf8"); //設置字符集
$sth->execute(array($link));
$re=$sth->rowCount(); //返回影響行數
return $re;
// $row=$sth->fetch(PDO::FETCH_ASSOC); //返回關聯
// $row=$sth->fetch(PDO::FETCH_NUM); //返回索引
// return $re; //返回影響行數
// return $row; //返回查詢到的一維數組
// 注:fetchALL()是返回二維數組
}
//模糊查詢
public function select_like($table,$condition,$link){ //表名,條件,數據
$sth = self::$obj->prepare("SELECT * FROM $table WHERE $condition LIKE ?"); //預處理
self::$obj->query("SET NAMES utf8"); //設置字符集
$sth->bindValue(1, "%$link%", PDO::PARAM_STR);
$sth->execute();
$row=$sth->fetchALL(PDO::FETCH_ASSOC); //返回關聯
// $row=$sth->fetch(PDO::FETCH_NUM); //返回索引
return $row; //返回查詢到的一維數組
// $re=$sth->rowCount(); //返回影響行數
// return $re; //返回影響行數
// 注:fetchALL()是返回二維數組
}
//查詢全部
public function select_s($table){ //表名
$sth = self::$obj->prepare("SELECT * FROM $table"); //預處理
$sth->execute();
$re=$sth->rowCount(); //返回影響行數
$row=$sth->fetchAll(PDO::FETCH_ASSOC); //返回二維數組關聯
// $row=$sth->fetchAll(PDO::FETCH_ASSOC); //返回二維數組關聯
// $row=$sth->fetchAll(PDO::FETCH_NUM); //返回二維數組索引
// return $re; //返回影響行數
return $row; //返回查詢到的一維數組
// 注:fetchALL()是返回二維數組
}
//用戶登陸的查詢
public function select_login($table,$user,$password,$user_link,$pass_link){ //表名,用戶名的字段,密碼的字段,用戶名的數據,密碼的數據
$sql="select * from $table where $user = ? and $password = ?";
$stmt = self::$obj->prepare($sql);
self::$obj->query("SET NAMES utf8"); //設置字符集
$stmt->execute(array($user_link, md5($pass_link))); //密碼是md5加密的
$re=$stmt->rowCount(); //返回影響行數
// $row=$sth->fetch(PDO::FETCH_ASSOC); //返回關聯
// $row=$sth->fetch(PDO::FETCH_NUM); //返回索引mysql
return $re; //返回影響行數
// return $row; //返回查詢到的一維數組
// 注:fetchALL()是返回二維數組
}
}
//類外調用靜態方法
//$model=Singleton::instance();
//$model->add();
?>sql