The PDO resource handle saved with a static property in the construction method cannot be called in another method?

Source: Internet
Author: User
Tags rowcount stmt
 Link};d bname={$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=> ',1=> ',2=> ' 12 ']); Echo '
';        Print_r ($stmt->fetchall (PDO::FETCH_ASSOC));    echo $stmt->rowcount ();            }//static method, single instance unified access portal static public function getinstance () {if (Is_null (self:: $DB) | | isset (self:: $DB)) {        Self:: $DB = new self ();    } return Self:: $DB; The Public Function Test () {$sql = ' SELECT * ' from user WHERE id=? and username=? and email=?         ';         $stmt = self:: $DB->prepare ($sql);         $stmt->execute ([0=> ',1=> ',2=> ' 12 ']); Echo '
';         Print_r ($stmt->fetchall (PDO::FETCH_ASSOC));         echo $stmt->rowcount ();    }} $db = Db::getinstance (); $db->test ();

I copied the test method in the construction method no problem, why in the test method will appear in the call to undefined method DB::p repare ()???
Please, God, you are great.


Reply to discussion (solution)

$DB->prepare does not have a prepare method, stating that $db may not be instantiated, print the $DB variable in the DB class after instantiation

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

In the GetInstance method, you are self:: $DB = new self (); The self:: $DB is assigned to a DB object, so there is no prepare method

You should at least write

Class db{protected $link = ' 127.0.0.1 ';    Protected $dbname = ' think ';    static public $DB;    static public $_db; Private Function __construct () {try{self::$_db = new PDO ("mysql:host={$this->link};d bname={$this-&gt        ;d bname} ", ' Root ', ' root ');        }catch (Pdoexception $e) {die ("Connection error:". $e->getmessage ()); }}//static method, single instance unified access portal static public function getinstance () {if (Is_null (self:: $DB) | | isset (self:: $DB        ) {self:: $DB = new self ();    } return Self:: $DB; The Public Function Test () {$sql = ' SELECT * ' from user WHERE id=? and username=? and email=?         ';         $stmt = Self::$_db->prepare ($sql);         $stmt->execute ([0=> ',1=> ',2=> ' 12 ']); Echo '
';         Print_r ($stmt->fetchall (PDO::FETCH_ASSOC));         echo $stmt->rowcount ();    }} $db = Db::getinstance (); $db->test ();
The PDO itself is well encapsulated and needs to be further encapsulated to simplify the calling code
Then you should inherit a DB class from PDO, such as
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);  }//executes various SQL instructions and can extend function query ($sql, $param =null) with parameter $param {$res = [];try {$rs = Parent::query ($sql);d o {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-&G  T;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::$_ins  Tance->query ($sql);//->fetchall (); if (count ($res) = = 1) return current ($res); }}
So you'll have a chance to use it.
$r = Db::fetch ("SELECT * from user where name= ' my ');

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

Thanks @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.