473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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



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
3 12793
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
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: André Somers | last post by:
Hi, I'm having a problem that I have a hard time understanding. I am getting a "Fatal error: Call to a member function on a non-object" on a constructor of a class, that looks like this...
3
by: Oliver Spiesshofer | last post by:
Hi, I am 'hacking' a bit on the opensource blog system Geeklog and while using a function of the code in another context, I get this error: Fatal error: Call to a member function on a...
1
by: zx.zhangxiong | last post by:
Dear all, I'm puzzled about the usage of class member function. Any help would be appreciated. class Account { ...
7
by: Ook | last post by:
What am I doing wrong? This code gives a compile error: 'SortedList<T>::insert' : illegal call of non-static member function. I've tried several variations of this, but keep getting the same error....
10
by: Thorsten Ottosen | last post by:
Hi, I'm trying to escape html before its saved in a database. I tried $text = htmlentities( $reader->value ); but that don't work for e.g. japanese characters.
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
5
by: loudwinston | last post by:
Hello, I'm encountering a strange error with PDO. The server is FreeBSD 4.4, PHP 5.1.2, mySQL 4.0.20 with 4.1.18 client libs. I have the following code (usernames and passwords changed to...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
2
by: mahesh | last post by:
Can anyone direct me to the place where i find the solution for the error message "cannot call member function 'X' without object"??? thanks in advance
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.