?
Solved

PHP:  Call to a member function prepare() on a non-object

Posted on 2010-01-04
7
Medium Priority
?
2,163 Views
Last Modified: 2012-05-08
Apologies to Delphi Zone: This is not for the Delphi Zone.

Hi

I'm trying to get someone's script working but keep on getting this error;
Fatal error: Call to a member function prepare() on a non-object

Below is the code, the problem is on this SECOND line:
            $pdo = DataDB::connect();
            $stmt = $pdo->prepare($sql);  <---- PROBLEM LINE
            $stmt->setFetchMode($mode);

Any ideas?

PHP is Version 5.1.6 running apache.
public function query($sql,$param=null,$mode = PDO::FETCH_ASSOC){
		if($this->sql_tracking===true){
			$querytrack = $sql;
			//if params used in sql, replace them into the sql string for logging
			if($param!=null){
				if(isset($param[0])){
					$querytrack = explode('?',$querytrack);
					$q = $querytrack[0];
					foreach($querytrack as $k=>$v){
						if($k===0)continue;
						$q .=  "'".$param[$k-1]."'" . $querytrack[$k];
					}
					$querytrack = $q;
				}else{
					//named param used
					foreach($param as $k=>$v)
					$querytrack = str_replace($k, "'$v'", $querytrack);
				}
			}
			$this->sql_list[] = $querytrack;
			echo "Query Exec: " . $querytrack . "<br>";
		}

		$pdo = DataDB::connect();
		$stmt = $pdo->prepare($sql);
		$stmt->setFetchMode($mode);


		if($param==null || empty($param) ){
			try{
				$stmt->execute();
				if($this->sql_tracking){
					echo "Row Affected : " . $stmt->rowCount() ."<br>";
				}
			}catch(PDOException $e){
				die("SQL Error: " . $e->getMessage() . "<br> Query: $sql");
			}
		}
		else{
			try{
				$stmt->execute($param);
				if($this->sql_tracking){
					echo "Row Affected : " . $stmt->rowCount() ."<br>";
				}
			}catch(PDOException $e){
				die("SQL Error: " . $e->getMessage() . "<br> Query: $sql and Values ".print_r($param,true));
			}
		}
		return $stmt;
	}

Open in new window

0
Comment
Question by:rfwoolf
  • 4
  • 3
7 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 26173596
from the problem description it sounds like DataDB::connect() is NOT returning a PDO object. Check your implentation of DataDB::connect();
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 26174086
hmmm interesting... here's the code for that
DataDB.php

<?php
class DataDB {
	const DB_TYPE_MYSQL = 1;
	const DB_TYPE_MSSQL = 2;


	const USE_PDO = true;
	const DB_TYPE = self::DB_TYPE_MSSQL;

	private static $instance = null;

	public function __construct(){
	}

	public static function connect(){

		if (!self::$instance)
		{
			if(self::USE_PDO){
				try{
					if(self::DB_TYPE == self::DB_TYPE_MYSQL){
						$dsn = 'mysql:dbname='.Configuration::db.';host='.Configuration::host;
						$user = Configuration::user;
						$password = Configuration::pass;
						self::$instance = new PDO($dsn, $user, $password);
						self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					}else if(self::DB_TYPE == self::DB_TYPE_MSSQL){
						self::$instance = new PDO("mssql:host=".Configuration::host.";dbname=".Configuration::db, Configuration::user ,Configuration::pass );
						self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					}
				}catch(PDOException $e){
					die ("Unable To Connect" . $e->getMessage());
				}
			}else{
				$db = mysql_connect(Configuration::host,Configuration::user,Configuration::pass);
				mysql_selectdb(Configuration::db,$db);
				self::$instance = $db;
			}
		}
		return self::$instance;

	}
}

Open in new window

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 2000 total points
ID: 26174498
Try:
DataDB.php

<?php
class DataDB {
        const DB_TYPE_MYSQL = 1;
        const DB_TYPE_MSSQL = 2;


        const USE_PDO = true;
        const DB_TYPE = self::DB_TYPE_MSSQL;

        private static $instance = null;

        public function __construct(){
        }

        public static function connect(){

                if (!self::$instance)
                {
                        if(self::USE_PDO){
                                try{
                                        if(self::DB_TYPE == self::DB_TYPE_MYSQL){
                                                $dsn = 'mysql:dbname='.Configuration::db.';host='.Configuration::host;
                                                $user = Configuration::user;
                                                $password = Configuration::pass;
                                                self::$instance = new PDO($dsn, $user, $password);
                                                self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                        }else if(self::DB_TYPE == self::DB_TYPE_MSSQL){
                                                self::$instance = new PDO("mssql:host=".Configuration::host.";dbname=".Configuration::db, Configuration::user ,Configuration::pass );
                                                self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                                        }
                                }catch(PDOException $e){
                                        die ("Unable To Connect" . $e->getMessage());
                                }
                        }else{
                                $db = mysql_connect(Configuration::host,Configuration::user,Configuration::pass);
                                mysql_selectdb(Configuration::db,$db);
                                self::$instance = $db;
                        }
                }
                return self::$instance;

        }
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:rfwoolf
ID: 26178062
No the problem persists :(
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 2000 total points
ID: 26180994
>>No the problem persists :(
That's because I pasted exactly what you posted! :)

Obviously, I pasted the wrong code. My apologies for that!

I tried what you posted but changed:

const DB_TYPE = self::DB_TYPE_MSSQL;

to:
const DB_TYPE = self::DB_TYPE_MYSQL;

Did you actually meant MSSQL? It seems odd to me that you have:
 if(self::USE_PDO){
...
}
else
{
 ///MYSQL code here
}
//but where is MSSQL code when USE_PDO=false???

Other than that, you Configuration class need to be made up of constants:
class Configuration{
      const db="...";
      const host="localhost";
      const user="...";
      const pass="...";
      
}
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 26181370
No I need MS SQL not MY SQL although the names are 1 letter difference.
I will show your code to the developer to ask him - I think he developed and tested on mysql, but says it should be able to work on mysql or mssql.
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 2000 total points
ID: 26182862
OK, then my other suggestion would be to try what's suggested here:
http://www.php.net/manual/en/ref.pdo-dblib.php#66917
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question