Storing Session Data in MySQL Table

Discussion in 'Programming/Scripts' started by Mosquito, Jul 13, 2007.

  1. Mosquito

    Mosquito New Member

    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:
    Code:
    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):
    Code:
    $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:
    Code:
    $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)
    Code:
    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?
     
  2. JimXL

    JimXL New Member

    Global?

    Is $connection a global variable?
     
  3. Mosquito

    Mosquito New Member

    Yes it is and used fine in another function
     
  4. falko

    falko Super Moderator Howtoforge Staff

    How did you initialize $connection?

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

    Mosquito New Member

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

    Code:
    Windows
    include_path = ".;c:\php\includes;c:\php\extras\libs;./include;C:\PHP\PEAR\pear"
    
    Linux
    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:
    Code:
    // Globals
    	@$connection; 
    	@$session_table; 
    
    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
    Code:
    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
    Code:
    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;
    }
    
     

Share This Page