PHP session_set_save_handler mysqli调用成员函数prepare()出错
PHP session_set_save_handler and mysqli call to a member function prepare() error
我需要使用 PHP 将会话写入数据库。所以我有这个 table:
CREATE TABLE sessions (
id varchar(32) NOT NULL,
access int(10) unsigned,
data text,
PRIMARY KEY (id)
);
和这个函数:
<?php
$hostname="localhost";
$titulo="config";
$user="root";
$pass="";
$bd="store";
$mysqli = new mysqli($hostname, $user, $pass, $bd) or die(mysqli_error());
session_set_save_handler('_open','_close','_read','_write','_destroy','_clean');
function _open() {
GLOBAL $mysqli;
}
function _close() {
GLOBAL $mysqli;
}
function _read($id) {
GLOBAL $mysqli;
$stmt = $mysqli->prepare("SELECT data FROM sessions WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$num = $result->num_rows;
if ($num>0) {
$record = $result->fetch_assoc();
return $record['data'];
}
return "";
}
function _write($id, $data) {
GLOBAL $mysqli;
$access = time();
$stmt = $mysqli->prepare("REPLACE INTO sessions VALUES (?,?,?)");
$stmt->bind_param('sss', $id, $access, $data);
$stmt->execute();
}
function _destroy($id) {
GLOBAL $mysqli;
$stmt = $mysqli->prepare("DELETE FROM sessions WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
}
function _clean($max) {
GLOBAL $mysqli;
$old = time() - $max;
$stmt = $mysqli->prepare("DELETE FROM sessions WHERE access < ?");
$stmt->bind_param('s', $old);
$stmt->execute();
}
?>
我的问题是,当我在上面包含此页面并像这样调用会话时:
session_start();
$_SESSION["user"] = "josh";
它没有记录在我的 sql table 中,通常会给我这个错误:
Fatal error: Call to a member function prepare() on a non-object
in C:\xampp\htdocs\session.php on line 41
第 41 行在 _write
函数中。
所以,我需要帮助才能让它工作,在会话 table 上记录这些数据。我的代码有什么问题?
编辑:
var_dump:
object(mysqli)#1 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $" ["client_version"]=> int(50011) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.6.16" ["server_version"]=> int(50616) ["stat"]=> string(136) "Uptime: 8072 Threads: 1 Questions: 41520 Slow queries: 0 Opens: 463 Flush tables: 1 Open tables: 85 Queries per second avg: 5.143" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(7756) ["warning_count"]=> int(0) }
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\session.php on line 43
试着把这行放在session_start()之前:
register_shutdown_function('session_write_close');
PHP manual recommends to do this. 这应该可以解决问题。
我需要使用 PHP 将会话写入数据库。所以我有这个 table:
CREATE TABLE sessions (
id varchar(32) NOT NULL,
access int(10) unsigned,
data text,
PRIMARY KEY (id)
);
和这个函数:
<?php
$hostname="localhost";
$titulo="config";
$user="root";
$pass="";
$bd="store";
$mysqli = new mysqli($hostname, $user, $pass, $bd) or die(mysqli_error());
session_set_save_handler('_open','_close','_read','_write','_destroy','_clean');
function _open() {
GLOBAL $mysqli;
}
function _close() {
GLOBAL $mysqli;
}
function _read($id) {
GLOBAL $mysqli;
$stmt = $mysqli->prepare("SELECT data FROM sessions WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$num = $result->num_rows;
if ($num>0) {
$record = $result->fetch_assoc();
return $record['data'];
}
return "";
}
function _write($id, $data) {
GLOBAL $mysqli;
$access = time();
$stmt = $mysqli->prepare("REPLACE INTO sessions VALUES (?,?,?)");
$stmt->bind_param('sss', $id, $access, $data);
$stmt->execute();
}
function _destroy($id) {
GLOBAL $mysqli;
$stmt = $mysqli->prepare("DELETE FROM sessions WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
}
function _clean($max) {
GLOBAL $mysqli;
$old = time() - $max;
$stmt = $mysqli->prepare("DELETE FROM sessions WHERE access < ?");
$stmt->bind_param('s', $old);
$stmt->execute();
}
?>
我的问题是,当我在上面包含此页面并像这样调用会话时:
session_start();
$_SESSION["user"] = "josh";
它没有记录在我的 sql table 中,通常会给我这个错误:
Fatal error: Call to a member function prepare() on a non-object
in C:\xampp\htdocs\session.php on line 41
第 41 行在 _write
函数中。
所以,我需要帮助才能让它工作,在会话 table 上记录这些数据。我的代码有什么问题?
编辑:
var_dump:
object(mysqli)#1 (19) { ["affected_rows"]=> int(0) ["client_info"]=> string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $" ["client_version"]=> int(50011) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(0) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.6.16" ["server_version"]=> int(50616) ["stat"]=> string(136) "Uptime: 8072 Threads: 1 Questions: 41520 Slow queries: 0 Opens: 463 Flush tables: 1 Open tables: 85 Queries per second avg: 5.143" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(7756) ["warning_count"]=> int(0) }
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\session.php on line 43
试着把这行放在session_start()之前:
register_shutdown_function('session_write_close');
PHP manual recommends to do this. 这应该可以解决问题。