我是这个主题的新手.我只是想连接我的数据库并获取数据.当使用静态连接时,它正在工作但不使用非静态.正如我所说,我对Php知之甚少,可能会遗漏一些简单的东西.
尝试获取非静态时出错由于发生内部服务器错误,无法显示页面.
我的代码
> <?php
class DB_Connect extends mysqli{
// protected static $connection;//working
protected $connection; / not working
function __construct() {
}
function __destruct() {
}
public function connect() {
if(!isset($this->$connection)) {
$config = parse_ini_file('./configOop.ini');
$this->$connection = new mysqli($config['dbhost'],$config['username'],$config['password'],$config['dbname']);
}
else{}
return $this->$connection;
/*
// using this part for static connection object, working
if(!isset(self::$connection)) {
$config = parse_ini_file('./configOop.ini');
self::$connection = new mysqli($config['dbhost'],$config['username'],$config['password'],$config['dbname']);
}
else{}
return self::$connection;
*/
}
// Closing database connection
public function close() {
// mysql_close();
}
}
?>
//
// error_reporting(0);
$db=new DB_Connect();
$dbConn=$db->connect();
if($result =$dbConn->query("Select * from cities")or die($dbConn->error)){
if($count=$result->num_rows){
while($row = $result->fetch_object())
{
echo $row->idcities;
}
}
}
?>
解决方法:
一个小小的错误:$this-> $connection应该是$this->连接.
PHP不需要第二个$,因为它已经知道(因为 – >)你指的是一个属性.如果你像你那样添加$,你基本上得到局部变量$connection的值,并使用该值作为属性名称.
标签:php,oop,mysqli
来源: https://codeday.me/bug/20190829/1762052.html