By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

php 5 call to member function prepare() on a non object error

P: n/a


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

<snippet_____________________>
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;
}
</_____________________________snippet>

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.

<snippet_____________________>
$stmnt = $dbh->prepare($sql);
$stmnt->execute();
$result = $s<tmnt->fetchAll();
return $result;
</_____________________________snippet>

Jan 3 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jan 3, 1:00 pm, Rowan <yaard...@gmail.comwrote:
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

<snippet_____________________>
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;
}
</_____________________________snippet>

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.

<snippet_____________________>
$stmnt = $dbh->prepare($sql);
$stmnt->execute();
$result = $s<tmnt->fetchAll();
return $result;
</_____________________________snippet>
This error is usually the result of $stmt not being reset to NULL or,
in the above case a syntax error "$result = $<stmnt->fetchALL();",
notice the errant < in $stmnt.

faulkes
Jan 3 '08 #2

P: n/a
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

<snippet_____________________>
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;
}
</_____________________________snippet>

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.

<snippet_____________________>
$stmnt = $dbh->prepare($sql);
$stmnt->execute();
$result = $s<tmnt->fetchAll();
return $result;
</_____________________________snippet>

Not enough code to tell what might be the problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 3 '08 #3

P: n/a
>
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.

<snippetclass 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>

<snippetclass 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>

Jan 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.