/    Sign up×
Community /Pin to ProfileBookmark

Database connection catching problem

Hi there,
I am using a script to cache statically DB and PDO statements executed.

here it is

[code=php]
<?php
require_once ‘ExceptionHandler.php’;
final class Database {

private static $db = “test”;
private static $host = “localhost”;
private static $username = “root”;
private static $password = “”;

private static $dbConn = null;
private static $queryCatch = array();
private static function CONNECT()
{
if(self::$dbConn === null)
{
$connUrl = “mysql:host=”.self::$host.”;dbname=”.self::$db;
self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT => true));
}
}

public static function query($sql)
{
Database::CONNECT();

if(isset(self::$queryCatch[$sql]) && is_object(self::$queryCatch[$sql]))
{
$query = self::$queryCatch[$sql];
}
else
{
$query = self::$dbConn->prepare($sql);
self::$queryCatch[$sql] = $query;
}

$numargs = func_num_args();
$arg_list = func_get_args();
//start from 1st parameter as 0th parameter is the query
for ($i = 1; $i < $numargs; $i++) {

if(is_int($arg_list[$i]))
{
$query->bindParam($i,$arg_list[$i],PDO::PARAM_INT);
}
else
{
$query->bindParam($i,$arg_list[$i],PDO::PARAM_STR);
}
}

$query->execute();

return $query;
}
}

?>
[/code]

I wanted to make this not just statically in page caching but a global $_SESSION caching

But the following changed to my connect method is not helping

[code=php]private static function CONNECT()
{
if(self::$dbConn === null)
{
if(isset($_SESSION[‘X_DB_CONN’]))
{
self::$dbConn = $_SESSION[‘X_DB_CONN’];echo “session cache hit”;
}
else
{
$connUrl = “mysql:host=”.self::$host.”;dbname=”.self::$db;
self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT => true));
$_SESSION[‘X_DB_CONN’] = self::$dbConn;
if(isset($_SESSION[‘test’]))
{
echo “:)”;
$_SESSION[‘test’] = “OO”;
}

echo “session cache NOT hit”;
}
}
}[/code]

I have started the session properly.
To Give proof and stating my problem:

The $_SESSION[‘test’] is set from another page to “?”. And

[QUOTE]

?

[/QUOTE]

is the output from the if statement on this page.

Beides always

[QUOTE]

session cache NOT hit

[/QUOTE]

is displayed

This is the output by the standard php error console

[QUOTE]

Fatal error: Exception thrown without a stack frame in Unknown on line 0

[/QUOTE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 26.2011 — Are you doing a session_start() before you call that object method?
Copy linkTweet thisAlerts:
@adarshakbauthorApr 26.2011 — yes i am using them
×

Success!

Help @adarshakb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...