欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php5.5 session_set_save_handler 连接数据库问题

程序员文章站 2022-03-23 15:13:38
...
好久前忘了在什么地方抄来的,一直好用,但是升级到PHP5.5就不好用了 出现警告
服务器无法修改PHP.ini 只好自己试着用mysqli写 但是一直写不出来 请高手指教!!
谢谢

function connect_db() {
$db_connect =
mysql_connect("host_name", "user_name", "password")
or die("Could not connect");
return $db_connect;
}


function open ($save_path, $session_name) {
global $db;
$db = connect_db();
return true;
}

function close() {
return true;
}

function read ($id) {
global $db;
mysql_select_db("db_name");

$result = mysql_query("SELECT *
FROM session_t
WHERE session_id='{$id}'");
if(mysql_num_rows($result) == 1){
$row = mysql_fetch_array($result);
return $row['session_data'];
} else {
return "";
}
}

function write ($id, $sess_data) {
global $db;
mysql_select_db("db_name");
$result = mysql_query("SELECT *
FROM session_t
WHERE session_id='{$id}'");
if(mysql_num_rows($result) == 1){
$result = mysql_query("UPDATE session_t
SET session_data='{$sess_data}'
WHERE session_id='{$id}'");
}else{
$date = date('Y-m-d H:i:s');
$result = mysql_query("INSERT INTO session_t
VALUES('{$id}' ,
'{$sess_data}' ,'{$date}')");
}
return true;
}

function destroy ($id) {
global $db;
mysql_select_db("db_name");
$result = mysql_query("DELETE from session_t
WHERE session_id='{$id}'");

return true;

}

function gc ($maxlife_time) {
return true;
}

session_set_save_handler
("open", "close", "read", "write", "destroy", "gc");
?>


回复讨论(解决方案)

把 mysql_ 都改成 mysqli_

>把 mysql_ 都改成 mysqli_
版主啊~ 那么改不行啊 错误更多了!!!!!
Warning: session_start(): Cannot send session cookie - headers already sent by
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in
Warning: mysqli_query() expects at least 2 parameters, 1 given in
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in

对不起 最后的Deprecated没有 粘错了

就等着你呢
出错是很正常的,改了就是了
不报错而又工作不正常,才是麻烦事

出错的 mysqli 函数是因为不能缺省数据库连接字
你把 $db 作为出错函数的第一个参数就可以了

OK解决了 谢谢版主