我在php数据库连接的时候报错,最后一句显示错误“Fatal error: Uncaught Error: Call to a member function connect() on null in /Applications/XAMPP/xamppfiles/htdocs/connect.php:38 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/connect.php on line 38”
<?php class Db{ static private $_instance; static private $_connectSource; private $_dbConfig = array( 'host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'stage', ); private function __construct(){ } static public function getInstance(){ if(self::$_instance instanceof self){ self::$_instance = new self(); } return self::$_instance; } public function connect(){ if(!self::$_connectSource){ self::$_connectSource = mysqli_connect($this->_dbConfig['host'],$this->_dbConfig['user'],$this->_dbConfig['password']); if(!self::$_connectSource){ die('mysql connect error'.mysql_error()); } mysqli_select_db(self::$_connectSource,$this->_dbConfig['database']); mysqli_query(self::$_connectSource,"set names UTF8"); echo "success"; } return self::$_connectSource; } } $connect = Db::getInstance() -> connect();
最后一句显示错误“Fatal error: Uncaught Error: Call to a member function connect() on null in /Applications/XAMPP/xamppfiles/htdocs/connect.php:38 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/connect.php on line 38”