<?php
header("content-type:text/html;charset=utf-8");
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();
}
//删除
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); //返回索引
return $re; //返回影响行数
// return $row; //返回查询到的一维数组
// 注:fetchALL()是返回二维数组
}
}
//类外调用静态方法
//$model=Singleton::instance();
//$model->add();
?>