4

It's the first time i'm using PDO just for testing purpose. But a strange error occurred and googling it, it seems to be weird.

Here's my database testing class

class db extends PDO { # Our instance. private static $db = NULL; # Calling the connector. public static function connect() { if (self::$db === NULL) { $class = __CLASS__; self::$db = new $class(); } return self::$db; } # Connector. public function __construct() { $dns = 'mysql:dbname='.reg::get('db-name').';host='.reg::get('db-host'); self::$db = new PDO($dns, reg::get('db-username'), reg::get('db-password')); reg::delete('db-password'); } # Quick reporting public function reportError($array) { if ($this->db != NULL) { echo 'Myself getting horny'; } // Just for testing, i'm not getting horny because of a mysql database connection! } } 

Then executing the following code:

$db = new db(); $row = $db->prepare('SELECT * FROM test WHERE id = :id')->execute(array('id' => 1)); echo $row['value']; 

It shows me the following error:

Warning: PDO::prepare() [pdo.prepare]: SQLSTATE[00000]: No error: PDO constructor was not called in myfile.php on line 39 

Considering line 39 as

$row = $db->prepare('SELECT * FROM test WHERE id = :id')->execute(array('id' => 1)); 
3
  • @andre matos, yes, i did. The same exactly error appears. Commented Jan 19, 2011 at 17:32
  • Why is the connect method there? Why are you checking if $db is null and not self::$db? Why are you doing self::$db = new db();. Commented Jan 19, 2011 at 17:34
  • Not your problem yet, but you can't use placeholders for table/columns names. Only for something that evaluates to a value. Commented Jan 19, 2011 at 17:46

4 Answers 4

5

You code is a mess, it's probably because you're so horny...

connect() method - why is it there?:

if ($db === NULL) 

should be:

if (self::$db === NULL) 

self::$db = new $class(); 

So, if $class == __CLASS__ == db, you are doing self::$db = new db();, doesn't seem right.


You can't use PDO to prepare an identifier, like a table or a column.

$db->prepare('SELECT * FROM :table WHERE id = :id')->execute(array('table' => 'test', 'id' => 1)); 

Should be:

$db->prepare('SELECT * FROM `test` WHERE id = :id')->execute(array('id' => 1)); 

Try this:

class db extends PDO { private static $db = null; public static function singleton() { if (is_null(self::$db) === true) { self::$db = new PDO('mysql:dbname='.reg::get('db-name').';host='.reg::get('db-host'), reg::get('db-username'), reg::get('db-password')); } return self::$db; } } 

Like this:

$result = db::singleton()->prepare('SELECT * FROM `test` WHERE id = :id')->execute(array('id' => 1)); var_dump($result); 
Sign up to request clarification or add additional context in comments.

6 Comments

I did corrected it but i still get the error. Also how am i supposed to be able to use all the PDO methods and be able to get the db singleton instance every time i want by calling db::singleton()/db::connect() ?
@Charlie Pigarelli: PS, you don't really need a class for this, a simple function would have the same effect: function singleton() { static $db = null; if (is_null($db)) { $db = new PDO(/*...*/); } return $db; }.
Tried but it shows me nothing. Neither if i var_dump() $result.
@Charlie Pigarelli: Weird. Try enabling error reporting by adding to following to the top of your script: error_reporting(-1); ini_set('display_errors', 1);. Also, is the reg class defined? Is the reg::get() method defined as static and returning something? Try replacing the reg::get() calls with valid values and try again.
Yeah, yeah, i found the error of the blank page by myself, it was not related to this. I re-tried your code and it returns 'bool(true)' that means it worked fine. Thanks. Last question before giving accepted answer: Why $result = db::singleton()->prepare('SELECT value FROM test WHERE id = :id')->execute(array('id' => 1))->fetch(); doesn't work?
|
2

I'm not entirely sure if this is your answer, as it seems to be unrelated to the error message, but I do not think you can pass in a table name as a bound parameter. What happens if you put a hard-coded table name in the place of :table?

Comments

1

You have a static 'connect' constructor, that makes a db-object in (static), and returns it. But you also make a new db() yourself.

The prepare statement uses self::$db, so tries to call the static made variable. I'm not really sure how your code is supposed to work, combining some sort of singleton/static form with an object form.

But that seems to be the trouble

1 Comment

Wait, we are not able to combine db::getInstance() (<- singleton?) and object form?
1

Your singleton pattern is all wrong. Your constructor should be private and you should use static instances of $db. Refer to this singleton example.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.