在构造方法中使用静态属性保留的PDO资源句柄在其他方法中无法调用?
程序员文章站
2022-05-16 17:18:34
...
在构造方法中使用静态属性保存的PDO资源句柄在其他方法中无法调用???
class DB{
protected $link = '127.0.0.1';
protected $dbname = 'think';
static public $DB;
private function __construct(){
try{
self::$DB = new PDO("mysql:host={$this->link};dbname={$this->dbname}",'root','root');
}catch (PDOException $e){
die("连接出错:".$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 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();
我把Test方法复制在构造方法没有问题,为什么在Test方法中会出现Call to undefined method DB::prepare()???
求各位大神了
------解决思路----------------------
你至少应写作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};dbname={$this->dbname}",'root','root');
}catch (PDOException $e){
die("连接出错:".$e->getMessage());
}
}
//静态方法,单例统一访问入口
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 '';PDO 本身已经封装的很好了,如确需要进一步封装以简化调用代码
print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
echo $stmt->rowCount();
}
}
$db = DB::getInstance();
$db->Test();
那么应该从 PDO 继承一个 DB 类,如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);
}
//执行各种 sql 指令,并可通过参数 $param 进行扩展
function query($sql, $param=null) {
$res = [];
try {
$rs = parent::query($sql);
do {
if($t = $rs->fetchall()) $res[] = $t;
}while($rs->nextRowset());
return $res;
} catch (PDOException $e) {
die( "Error!: " . $e->getMessage() . "\n" );
// die();
}
}
//查询并返回单条记录
static function fetch($sql) {
if(! self::$_Instance) self::$_Instance = new self;
return self::$_Instance->query($sql)[0][0];//->fetch();
}
//查询并以数组方式返回多条记录
static function fetchall($sql) {
if(! self::$_Instance) self::$_Instance = new self;
$res = self::$_Instance->query($sql);//->fetchall();
if(count($res) == 1) return current($res);
}
}
$r = DB::fetch("select * from user where name='my'");相关文章
相关视频
上一篇: mysql密码忘记的解决方法(图)