Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 13th July 2007, 14:03
Mosquito Mosquito is offline
Member
 
Join Date: Nov 2006
Posts: 85
Thanks: 5
Thanked 6 Times in 5 Posts
Default 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:
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?
Reply With Quote
Sponsored Links
  #2  
Old 13th July 2007, 16:56
JimXL JimXL is offline
Junior Member
 
Join Date: Jul 2007
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
Default Global?

Is $connection a global variable?
Reply With Quote
  #3  
Old 13th July 2007, 17:21
Mosquito Mosquito is offline
Member
 
Join Date: Nov 2006
Posts: 85
Thanks: 5
Thanked 6 Times in 5 Posts
Default

Yes it is and used fine in another function
Reply With Quote
  #4  
Old 14th July 2007, 13:12
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,735 Times in 2,571 Posts
Default

How did you initialize $connection?

Is the include_path the same in php.ini on your windows and Linux system?
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #5  
Old 14th July 2007, 21:37
Mosquito Mosquito is offline
Member
 
Join Date: Nov 2006
Posts: 85
Thanks: 5
Thanked 6 Times in 5 Posts
Default

Quote:
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.

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;
}
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Statistic not working mzo Installation/Configuration 49 20th April 2011 12:19
Help with Bluetooth PAND josto HOWTO-Related Questions 1 7th August 2007 20:53
mysql clustering problem : cannot create table in database prince.fer Server Operation 2 16th April 2007 17:03
Website users? ctroyp General 25 6th January 2006 18:02


All times are GMT +2. The time now is 09:48.


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