You are here: Re: php 5 call to member function prepare() on a non object error « PHP Programming Language « IT news, forums, messages
Re: php 5 call to member function prepare() on a non object error

Posted by Rowan on 01/03/08 20:20

>
> Not enough code to tell what might be the problem.


ok let's see if I can make this a little clearer. The basic purpose of
the script is to cross check a login via pqsql, if the login is valid
record the session on the database then set $_SESSION.

when I run a test script I get the following error.
PHP Fatal error: Call to a member function prepare() on a non-object
in /usr/local/lib/php5/pg_connect.php on line 76

ck_session::s_session is called once a previous function test the
login using db_cxn::db_select. once that returns success then
s_session tries to INSERT using db_cxn::db_update which is essentially
the same routine as before. I'm new to this oo stuff. Ideally I would
like to create one persistent object for the db connection and keep
reusing it. I think my approach is wrong.

<snippet> class ck_session
private static function s_session($s_info) {
$_SESSION = array(); // making sure existing sessions are voided.
$s3net_session['usr_session_start']= time();
$s3net_session['usr_id'] = $s_info['usr_id'];
$s3net_session['usr_name'] = $s_info['usr_lgn'];
$s_key = $s3net_session['usr_session_start'] .
$s3net_session['usr_id'];
$s3net_session['usr_session_key'] = crypt($s_key);
$s3net_session['usr_session_active'] = "true";
if (!$cxn) {
print "ck_session::s_session() created a new cxn::<br/>";
$cxn = new db_cxn;
}
$sql = $cxn->c_statement($s3net_session);
print "$sql <br/>";
if ($cxn->db_update($sql)){
foreach ($s3net_session as $key => $value){
$_SESSION[$key] = $value;
}
return 1;
}else{
return 0;
}


}
</snippet>

<snippet> class db_cxn
{
private static $db;
public static $dsn;
public static $user;
public static $pass;
public static $driverOpts;


private static function cxn() {
self::$dsn = 'pgsql:host=localhost port=5432 dbname=s3net';
self::$user = $_SERVER[PG_USER];
self::$pass = $_SERVER[PG_USER_PW];
self::$driverOpts = null;
try {
if (is_null(self::$db)) {
self::$db = new PDO(self::$dsn, self::$user, self::$pass);
return self::$db;
}
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}



public static function db_select($sql) {
$str_check = explode(" ", $sql);
if($str_check[0] == "SELECT") {
if (!$dbh){
$dbh = self::cxn();
print 'db_cxn::db_select::> a new instance of $dbh <br/>';
}
$stmnt = $dbh->prepare($sql);
$stmnt->execute();
$result = $stmnt->fetchAll();
$stmnt = NULL;
$sql = NULL;
return $result;
}else{
print "Error!: Incorrectly Formatted SQL Statement";
}
}

public static function db_update($sql) {
$str_check = explode(" ", $sql);
if($str_check[0] == "INSERT") {
if (!$dbh){
$dbh = self::cxn();
print 'db_cxn::db_update::> a new instance of $dbh <br/>';
}
print "$sql <br/>";
LINE 76 ---> $stmnt = $dbh->prepare($sql);
$stmnt = NULL;
$sql = NULL;
return 1;
}else{
print "Error!: Incorrectly Formatted SQL Statement";
}
}

}
</snippet>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация