Problem connecting to PDO

This is the first time I use PDO for testing purposes only. But a strange mistake happened and ruined it, it seems strange.

Here is 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 execute the following code:

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

He 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 

Given line 39 how

 $row = $db->prepare('SELECT * FROM test WHERE id = :id')->execute(array('id' => 1)); 
+4
source share
4 answers

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

connect() method - why is it there ?:

 if ($db === NULL) 

it should be:

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

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

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


You cannot use PDO to prepare an identifier, such as a table or column.

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

Must be:

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

Try the following:

 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); 
+5
source

I am not quite sure if this is your answer, since it does not seem to be related to the error message, but I do not think you can pass the table name as a related parameter. What happens if you put the name of a hard-coded table instead of :table ?

+2
source

You have a static "connect" constructor that makes a db object in (static) and returns it. But you also create a new db () yourself.

The prepare statement uses self::$db , so it tries to call a static variable. I'm not quite sure how your code should work by combining some singleton / static form with the shape of an object.

But that seems to be the problem

+1
source

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

+1
source

Source: https://habr.com/ru/post/1336178/


All Articles