mysql prepare例子_mysqli prepare 单类的简单例子

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;

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值