The PDO resource handle saved using static properties in the constructor cannot be called in other methods?

Source: Internet
Author: User
The PDO resource handle saved using static properties in the constructor cannot be called in other methods?
 Link}; dbname = {$ this-> dbname} ", 'root', 'root');} catch (PDOException $ e) {die (" connection error :". $ e-> getMessage ();} $ SQL = 'select * FROM user WHERE id =? And username =? And email =? '; $ Stmt = self: $ DB-> prepare ($ SQL); $ stmt-> execute ([0 => '13', 1 => '12 ', 2 => '12']); echo'
'; Print_r ($ stmt-> fetchAll (PDO: FETCH_ASSOC); echo $ stmt-> rowCount ();} // static method, single-instance unified access entry static public function getInstance () {if (is_null (self ::$ DB) | isset (self ::$ DB) {self :: $ DB = new self ();} return self: $ DB;} public function Test () {$ SQL = 'select * FROM user WHERE id =? And username =? And email =? '; $ Stmt = self: $ DB-> prepare ($ SQL); $ stmt-> execute ([0 => '13', 1 => '12 ', 2 => '12']); echo'
';         print_r($stmt->fetchAll(PDO::FETCH_ASSOC));         echo $stmt->rowCount();    }}$db = DB::getInstance();$db->Test();

I copied the Test method to the constructor. Why does Call to undefined method DB: prepare ()? appear in the Test method ()???
Thank you!


Reply to discussion (solution)

$ DB-> prepare does not have the prepare method, which means $ DB may not be instantiated. After instantiation, print the $ DB variable in the DB class.

In the constructor, self: $ DB is a PDO object, so there is a prepare method.

In the getInstance method, you assign the self: $ DB = new self (); self: $ DB to the DB object, so the prepare method is absent.

You should at least write

Class DB {protected $ link = '2017. 0.0.1 '; protected $ dbname = 'think'; static public $ DB; static public $ _ DB; private function _ construct () {try {self :: $ _ DB = new PDO ("mysql: host = {$ this-> link}; dbname = {$ this-> dbname}", 'root ', 'root');} catch (PDOException $ e) {die ("connection error :". $ e-> getMessage () ;}// static method, single-instance unified access entry static public function getInstance () {if (is_null (self: $ DB) | isset (self: $ DB )){ Self: $ DB = new self ();} return self: $ DB;} public function Test () {$ SQL = 'select * FROM user WHERE id =? And username =? And email =? '; $ Stmt = self: $ _ DB-> prepare ($ SQL); $ stmt-> execute ([0 => '13', 1 => '12 ', 2 => '12']); echo'
';         print_r($stmt->fetchAll(PDO::FETCH_ASSOC));         echo $stmt->rowCount();    }}$db = DB::getInstance();$db->Test();
PDO itself is well encapsulated, and further encapsulation is required to simplify the calling code.
A db class should be inherited from PDO, such
Class DB extends PDO {private static $ _ Instance; function _ construct () {$ options = array (PDO: MYSQL_ATTR_INIT_COMMAND => "set names gbk", PDO :: ATTR_ERRMODE => PDO: ERRMODE_EXCEPTION, PDO: ATTR_DEFAULT_FETCH_MODE => PDO: FETCH_ASSOC,); parent :__ construct ('MySQL: dbname = test', 'root ', '', $ options);} // execute various SQL commands and use the $ param parameter to extend the function query ($ SQL, $ param = null) {$ res = []; try {$ rs = parent: que Ry ($ SQL); do {if ($ t = $ rs-> fetchall () $ res [] = $ t ;} while ($ rs-> nextRowset (); return $ res;} catch (PDOException $ e) {die ("Error! :". $ E-> getMessage (). "\ n"); // die () ;}// query and return a single record static function fetch ($ SQL) {if (! Self: $ _ Instance) self: $ _ Instance = new self; return self: $ _ Instance-> query ($ SQL) [0] [0]; //-> fetch ();} // query and return multiple records in array mode static function fetchall ($ SQL) {if (! Self: $ _ Instance) self ::$ _ Instance = new self; $ res = self ::$ _ Instance-> query ($ SQL ); //-> fetchall (); if (count ($ res) = 1) return current ($ res );}}
This gives you the opportunity to use it like this.
$ R = DB: fetch ("select * from user where name = 'my '");

        if (!self::$DB instanceof self) {            self::$DB = new self ();        }        return self::$DB;

Thank you @ xuzuning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.