HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (
-   Programming/Scripts (
-   -   Storing Session Data in MySQL Table (

Mosquito 13th July 2007 15:03

Storing Session Data in MySQL Table
Hello all,

I am having an odd problem that I was hoping you could help with. I have a PHP application that stores session data into a MySQL table. This process works fine on Windows Server 2003. I am now migrating this application to a linux server. Since the migration, it fails to work at all.

I continue to receive this message in my error logs:

PHP Fatal error:  Call to a member function query() on a non-object in /path/to/file/file.php on line 83
Line 83 looks like this (in Write_Session):

$result = $connection->query("SELECT session_id FROM $session_table WHERE session_id = '$sess_id'");
Now, normally, I would assume that their is a problem with the connection object. However, it is set globally and is used just fine in my Read_Session function:

$result = $connection->query("SELECT * FROM $session_table WHERE session_id = '$sess_id'");
I have compared the php.ini files of the Windows machine and the linux machine. Aside from the obvious linux/windows differences, all of my paths, variables, etc. are set correctly.

I am using (in case this helps)

PHP 5.2.2 (cli) (built: May  8 2007 08:15:05)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

Does anyone have any ideas?

JimXL 13th July 2007 17:56

Is $connection a global variable?

Mosquito 13th July 2007 18:21

Yes it is and used fine in another function

falko 14th July 2007 14:12

How did you initialize $connection?

Is the include_path the same in php.ini on your windows and Linux system?

Mosquito 14th July 2007 22:37


Originally Posted by falko
How did you initialize $connection?

Is the include_path the same in php.ini on your windows and Linux system?

The only include that is different between the two environments is the addition of the Smarty Templating directory on Linux.


include_path = ".;c:\php\includes;c:\php\extras\libs;./include;C:\PHP\PEAR\pear"

include_path = ".:/usr/share/php:/usr/local/lib/php/Smarty/:/usr/share/pear/:./include"

To initialize, the connection is declared as global. In the begin_session function, the connection is set up. This connection works fine if I am just trying to read the session data, but if I try to write to the session table, the query function is unknown.

This is the Begin_Session function:

// Globals

Begin_Session($db_name, $tb_name)
        global $connection;
        global $session_table;
        $connection = new db_conn($db_name);
        $session_table = $tb_name;
        return true;

This is the Read_Session function

function Read_Session($sess_id)
        global $connection;
        global $session_table;
        $query_result = $connection->query("SELECT * FROM $session_table WHERE session_id = '$sess_id'");
        if(mysql_num_rows($query_result) == 0)       
                return ""; // No session exists       
        else  // Session exists. Yay!                       
                return mysql_result($result,0,1);

This is the Write_Session function

function Write_Session($sess_id, $add_to_session)
        global $connection;
        global $session_table;
        $seconds = getMicroTime();    // This function just returns seconds since 1970
        $query_result = $connection->query("SELECT session_id FROM $session_table WHERE session_id = '$sess_id'");
        $add_to_session = addslashes($add_to_session);
        if(mysql_num_rows($query_result) == 0) // If no session exists, make this a new session
                $connection->query("INSERT INTO $session_table (session_id, session_variable, last_accessed) VALUES('$sess_id','$add_to_session',$seconds)");
        else // Session already exists, so update session data
                $connection->query("UPDATE $session_table SET session_variable = '$add_to_session', last_accessed = $seconds WHERE session_id = '$sess_id'");
        return true;

All times are GMT +2. The time now is 18:11.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.