Config.php
<?php
//Database Connection constants
define('DB_HOST' , 'localhost');
define('DB_USER' , 'root');
define('DB_PASS' , '');
define('DB_NAME' , 'gallery_db');
?>
database.php中
<?php
require_once ("config.php");
class Database {
public $connection;
function __construct() {
$this->open_db_connection();
}
public function open_db_connection () {
$this->connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(mysqli_connect_errno()){
die("database connection failed badly" . mysqli_error());
}
}
}
// $database = new Database();
$database = new Database();
?>
<?php
include "config.php";
include "database.php";
?>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Blank Page
<small>Subheading</small>
</h1>
<?php
if($database->connection) // the line that the error is pointing to
{
echo "true";
}
?>
<ol class="breadcrumb">
<li>
<i class="fa fa-dashboard"></i> <a href="index.html">Dashboard</a>
</li>
<li class="active">
<i class="fa fa-file"></i> Blank Page
</li>
</ol>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
代码在同一个文件上工作正常,但是当我尝试从不同的文件/ scipt访问公共变量时,会出现以下错误(2)
注意:未定义的变量:数据库中 第15行的C:\ xampp \ htdocs \ gallery \ admin \ includes \ admin_content.php
注意:尝试获取非对象的属性 第15行的C:\ xampp \ htdocs \ gallery \ admin \ includes \ admin_content.php
答案 0 :(得分:1)
您忘记在init.php
admin_content.php
个文件
在admin_content.php
文件
<?php include 'init.php'; ?>
注意: - 您不需要在config.php
文件中包含init.php
文件,因为该文件已加载到database.php
文件中。
答案 1 :(得分:1)
<强>的init.php 强>
<?php
include "config.php";
include "database.php";
?>
编辑(init.php)
<?php
include "config.php";
include "Database.php";
?>
<强> admin_content.php 强>
//include init.php file
<?php include 'init.php' ?>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Blank Page
<small>Subheading</small>
</h1>
<?php
if($database->connection) // the line that the error is pointing to
{
echo "true";
}
?>
<ol class="breadcrumb">
<li>
<i class="fa fa-dashboard"></i> <a href="index.html">Dashboard</a>
</li>
<li class="active">
<i class="fa fa-file"></i> Blank Page
</li>
</ol>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>