static properties can contain resources? [resolved. at last]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

static properties can contain resources? [resolved. at last]

Post by Jenk »

hihi..

following from my embarrassing session handler thread, I'm now getting errors reported ala below:
Warning: mysql_real_escape_string(): 7 is not a valid
MySQL-Link resource in E:\Program Files\Apache
Group\Apache2\htdocs\stkmart\includes\classes\jmt_Session.class.php
on line 43
for all mysql_* funcs within the class (see link to other thread for code)

This leads me to belive static properties cannot contain resources? Even though it works, it still gives the errors?!

(nb: if I remove self::$link from all the mysql_* funcs, it ceases to function.)

Much obliged :)
Last edited by Jenk on Wed Aug 02, 2006 8:15 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I've tried by ref as well, but it still generates the warning (and also generates the 'only variables should be assigned by reference' E_STRICT)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Sounds like the connection isn't being made to me.
Change this:

Code: Select all

public static function open ()
    {
        if (self::$link = mysql_connect(self::$db['HOST'], self::$db['USER'], self::$db['PASS'])) {
            return mysql_select_db(self::$db['DATABASE'], self::$link);
        } else {
            return false;
        }
    }
To throw exceptions (pref.) or trigger_errors.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

The connection is being made. Hence it having a resource id of 7 and not stating false.

I can also see this is true because the session is working, session data is being read/written in the DB and on screen, however I still get the warnings.

Code: Select all

public static function open ()
    {
        
        if (self::$link = mysql_connect(self::$db['HOST'], self::$db['USER'], self::$db['PASS'])) {
            var_dump(self::$link);
            return mysql_select_db(self::$db['DATABASE'], self::$link);
        } else {
            return false;
        }
    }

Code: Select all

resource(7) of type (mysql link)
however..

Code: Select all

public static function close ()
    {
        var_dump(self::$link);
        return mysql_close(self::$link);
    }

Code: Select all

resource(7) of type (unknown)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Except list($data)=... it works fine for me.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

wrong thread?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No, I took the two scripts from viewtopic.php?t=52676 and they work fine.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

oh right, yes they do 'work.'

I wanted to see if I could clear the warning errors.

Thank you for testing :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Ok, let me rephrase that.
Both scripts work - as in 'work without any error or warning' ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

They don't here :/
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

line 43 is which one exactly?


Maybe some error_log entries will help to find the error

Code: Select all

<?php
class jmt_Session
{
	private static $db = array();
	private static $link;

	public static function set(array $db)
	{
		self::$db = $db;
	}

	public static function open()
	{
		
		if (self::$link = mysql_connect(self::$db['HOST'], self::$db['USER'], self::$db['PASS'])) {
			$retval = mysql_select_db(self::$db['DATABASE'], self::$link);
		} else {
			$retval = false;
		}
		error_log('jmt_Session::open '.(($retval)?'success':mysql_error()));
		return $retval;
	}

	public static function close ()
	{
		return mysql_close(self::$link);
	}

	public static function read($id)
	{ 
		$id = mysql_real_escape_string($id, self::$link);
		$sql = "SELECT `data` FROM `session` WHERE `sessionId` = '$id'";
		error_log('jmt_Session::read '.$sql);
		if ($result = mysql_query($sql, self::$link)) {
			$row = mysql_fetch_assoc($result);
			error_log('jmt_Session::read '.$row['data']);
			return $row['data'];
		} else {
			error_log('jmt_Session::read no data');
			return '';
		}
	}

	public static function write($id, $data)
	{
		$access = time();

		$id = mysql_real_escape_string($id, self::$link);
		$data = mysql_real_escape_string($data, self::$link);

		$sql = "REPLACE 
			INTO `session` (`sessionId`, `access`, `data`)
			VALUES ('$id', '$access', '$data')";
		error_log('jmt_Session::write '.$sql);
		return mysql_query($sql, self::$link);
	}

	public static function destroy ($id)
	{
		$id = mysql_real_escape_string($id, self::$link);
		$sql = "DELETE FROM `session` WHERE `sessionId` = '$id'";
		return mysql_query($sql, self::$link);
	}

	public static function clean($max)
	{     
		$old = time() - $max;
		
		$sql = "DELETE
			FROM   `session`
			WHERE  `access` < '$old'";
			return mysql_query($sql, self::$link);
	}
}
?>
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

public static function write($id, $data)
        {
                $access = time();

                $id = mysql_real_escape_string($id, self::$link); //this line
however, that is only one of the messages. I receive a warning on every line that self::$link is used, except for where it is set.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And what about the error_logs?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

the same.
Post Reply