есть три файла:
config.php
<?php
class config {
public $host = "localhost";
public $user = "root";
public $password = "";
public $dbname = "test";
}
?>
файл db.php - для подключения к базе
<?php
require_once "config.php";
class db extends config {
private $connection;
public static $mysqli = null;
function __construct() {
$this->open_connection();
$this->connection->query("SET NAMES 'utf8'");
//echo "OK";
}
private function open_connection() {
$this->connection = $mysqli = new mysqli($this->host, $this->user, $this->password, $this->dbname);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
}
public static function getObject() {
if(self::$mysqli) {
$obj = new db();
self::$mysqli = $obj->connection;
}
return self::$mysqli;
}
}
$db = new db();
?>
файл home.php - файл - вывод
<?php
require_once "classes/db.php";
$mysqli = db::getObject();
$sql = "SELECT content FROM pages";
$result = $mysqli->query($sql);
if($result && $result->num_rows > 0) {
$row = $mysqli->assoc($result);
do
{
echo $row['content'];
}
while($row = $mysqli->assoc($result));
}
else {
echo "<p>Пустой контент</p>";
}
?>
выдает ошибку: Call to a member function query() on a non-object in home.php on line 7
вот этот лайн:
$result = $mysqli->query($sql);