class mysqli_con{
private $conn;//musqli链接
public static $_instance = null;//单类静态变量
/**
构造函数
mysqli的connect
*/
private function __construct(){
require './db.config.php';
$this->conn = new MySQLi($dbarray['host'],$dbarray['user'],$dbarray['password'],$dbarray['database']);
if(mysqli_connect_errno()){
die('Unable to connect').mysqli_connect_error();
}
$this->conn->set_charset("utf8");
}
private function __clone(){
die('clone is not allowed');
}
/*
* 单类模式
*
* */
public static function getInstance(){
if(self::$_instance === null){
self::$_instance = new self();
}
return self::$_instance;
}
/*
*预处理数据库
*
* */
public function pre_sel($number){
$sql = "select id,user,pwd,number,submittime from userinfo where number=? limit 1";
$mysqli_stmt = $this->conn->prepare($sql);//预处理数据库
$mysqli_stmt->bind_param("s",$number);//绑定变量
$mysqli_stmt->bind_result($id,$user,$pwd,$number,$submittime);//绑定结果集
$mysqli_stmt->execute();
$mysqli_stmt->store_result();
if($mysqli_stmt->num_rows>0){
while($mysqli_stmt->fetch()){
$data['id'] = $id;
$data['user']=$user;
$data['pwd']=$pwd;
$data['number']=$number;
$data['submittime']=$submittime;
}
// return $data;
echo json_encode($data);
$mysqli_stmt->free_result();
$mysqli_stmt->close();
// $this->close();
}else{
$submittime = time();
$sql_kong = "select * from userinfo where number=0 order by id limit 1";
$result = $this->conn->query($sql_kong);
while($rows = $result->fetch_assoc()){
$data_kong['id'] = $rows['id'];
$data_kong['user']=$rows['user'];
$data_kong['pwd']=$rows['pwd'];
}
echo json_encode($data_kong);
$id = $data_kong['id'];
$up_sql = "update userinfo set number=?,submittime=$submittime where id =$id ";
$mysqli_stmt = $this->conn->prepare($up_sql);
$mysqli_stmt->bind_param("s",$number);
$mysqli_stmt->execute();
$mysqli_stmt->free_result();
$mysqli_stmt->close();
// $this->close();
}
}
/*
* 关闭数据库
* 静态变量清空
* */
public function close(){
$this->conn->close();
self::$_instance = null;
}
}
?>