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-> ;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 asClass 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.