Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_close(): supplied argument is not a valid MySQL-Link resource

Tags:

php

mysql

I'm trying to get the hang of using custom session handlers to store session data in a MySQL database. However, I keep getting the following warning:

mysql_close(): supplied argument is not a valid MySQL-Link resource

Here's the code I'm using, which I got from here:

function _open(){

    global $_sess_db;
    $_sess_db = mysql_connect("localhost", "root", "******");
    if ($_sess_db) {
        return mysql_select_db('style', $_sess_db);
    }
    return false;
}

function _close(){
    global $_sess_db;
    return mysql_close($_sess_db); //error happens here
}

The full text of the error message ultimately points to the final "return mysql_close($_sess_db);" line. I can confirm that the mysql_connect info does in fact work, and I do have the rest of the session handler functions defined as well.

And in case it helps, I get these errors immediately upon page load, without actually calling any of the session handler functions, and without having any current sessions open.


like image 306
maxedison Avatar asked Dec 17 '25 16:12

maxedison


1 Answers

the problem is your variable scope. the global keyword does nothing if the variable was not defined outside of a function first. an alternative would be to use the super global $GLOBALS['_sess_db'], or to first define $_sess_db outside of any functions.

like image 131
dqhendricks Avatar answered Dec 19 '25 04:12

dqhendricks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!