php 5 call to member function prepare() on a non object error
php 5 call to member function prepare() on a non object error
am 03.01.2008 19:00:38 von Rowan
When I run a small 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 73
public static function db_update($sql) {
$str_check = explode(" ", $sql);
if($str_check[0] == "INSERT") {
if (!isset($dbh)){
$dbh = self::cxn();
}
line 73 ---> $stmnt = $dbh->prepare($sql);
$stmnt->execute();
return 1;
}
this is part of a class called db_cxn which is being called by the
following statement from a separate class.
-->if ($cxn->db_update($sql)){
whats weird is there is a previous call to a very similar function
below which works fine. can anyone point me in the right direction ?
fyi both objects are created as such --->$dbh = self::cxn(); where
$dbh is private static.
$stmnt = $dbh->prepare($sql);
$stmnt->execute();
$result = $sfetchAll();
return $result;
Re: php 5 call to member function prepare() on a non object error
am 03.01.2008 19:43:22 von faulkes
On Jan 3, 1:00 pm, Rowan wrote:
> When I run a small 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 73
>
>
> public static function db_update($sql) {
> $str_check = explode(" ", $sql);
> if($str_check[0] == "INSERT") {
> if (!isset($dbh)){
> $dbh = self::cxn();
> }
> line 73 ---> $stmnt = $dbh->prepare($sql);
> $stmnt->execute();
> return 1;
> }
>
>
> this is part of a class called db_cxn which is being called by the
> following statement from a separate class.
> -->if ($cxn->db_update($sql)){
>
> whats weird is there is a previous call to a very similar function
> below which works fine. can anyone point me in the right direction ?
>
> fyi both objects are created as such --->$dbh = self::cxn(); where
> $dbh is private static.
>
>
> $stmnt = $dbh->prepare($sql);
> $stmnt->execute();
> $result = $sfetchAll();
> return $result;
>
This error is usually the result of $stmt not being reset to NULL or,
in the above case a syntax error "$result = $fetchALL();",
notice the errant < in $stmnt.
faulkes
Re: php 5 call to member function prepare() on a non object error
am 03.01.2008 19:50:47 von Jerry Stuckle
Rowan wrote:
>
> When I run a small 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 73
>
>
> public static function db_update($sql) {
> $str_check = explode(" ", $sql);
> if($str_check[0] == "INSERT") {
> if (!isset($dbh)){
You never set $dbh here, so this has to be true.
> $dbh = self::cxn();
>
Obviously, self::cxn() is not returning an object.
}
> line 73 ---> $stmnt = $dbh->prepare($sql);
> $stmnt->execute();
> return 1;
> }
>
>
> this is part of a class called db_cxn which is being called by the
> following statement from a separate class.
> -->if ($cxn->db_update($sql)){
>
> whats weird is there is a previous call to a very similar function
> below which works fine. can anyone point me in the right direction ?
>
> fyi both objects are created as such --->$dbh = self::cxn(); where
> $dbh is private static.
>
>
> $stmnt = $dbh->prepare($sql);
> $stmnt->execute();
> $result = $sfetchAll();
> return $result;
>
>
>
Not enough code to tell what might be the problem.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: php 5 call to member function prepare() on a non object error
am 03.01.2008 21:20:23 von Rowan
>
> 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.
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::
";
$cxn = new db_cxn;
}
$sql = $cxn->c_statement($s3net_session);
print "$sql
";
if ($cxn->db_update($sql)){
foreach ($s3net_session as $key => $value){
$_SESSION[$key] = $value;
}
return 1;
}else{
return 0;
}
}
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() . "
";
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
';
}
$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
';
}
print "$sql
";
LINE 76 ---> $stmnt = $dbh->prepare($sql);
$stmnt = NULL;
$sql = NULL;
return 1;
}else{
print "Error!: Incorrectly Formatted SQL Statement";
}
}
}