maxedison
maxedison

Reputation: 17553

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

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.


Upvotes: 3

Views: 5021

Answers (2)

dqhendricks
dqhendricks

Reputation: 19251

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.

Upvotes: 3

Stefan H Singer
Stefan H Singer

Reputation: 5504

Try adding some debug printouts in _open() and _close() and see if they are called when you're not expecting them to be.

Upvotes: 0

Related Questions