【发布时间】:2016-02-16 02:12:18
【问题描述】:
我是这个主题的新手。我只是想连接我的数据库并获取数据。使用静态连接时,它可以工作,但不能使用非静态连接。正如我所说,我对 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();
}
}
?>
//
<?php include 'db_connectOop.php'; ?>
<?php
// 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;
}
}
}
?>
【问题讨论】: