[英]I am trying to use the public variable in another scipt in PHP, but im getting two errors
Config.php 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
<?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>
the code is working fine on the same file, but following (2) erors are there when i try to access the public variable from a different file/scipt 该代码在同一文件上工作正常,但是当我尝试从其他文件/密码访问公共变量时,出现以下(2)错误
Notice: Undefined variable: database in C:\\xampp\\htdocs\\gallery\\admin\\includes\\admin_content.php on line 15
注意:未定义的变量:第15行的C:\\ xampp \\ htdocs \\ gallery \\ admin \\ includes \\ admin_content.php中的数据库
Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\gallery\\admin\\includes\\admin_content.php on line 15
注意:尝试在第15行的C:\\ xampp \\ htdocs \\ gallery \\ admin \\ includes \\ admin_content.php中获取非对象的属性
You forgot to include init.php
file in admin_content.php
您忘记在
admin_content.php
包含init.php
文件
Write below line in top of your admin_content.php
file 在
admin_content.php
文件顶部的以下行中admin_content.php
<?php include 'init.php'; ?>
Note:- You do not need to include config.php
file in your init.php
file because that file is already loaded in database.php
file. 注意:-您不需要在
init.php
文件中包含config.php
文件,因为该文件已经加载在database.php
文件中。
init.php init.php
<?php
include "config.php";
include "database.php";
?>
edit (init.php) 编辑(init.php)
<?php
include "config.php";
include "Database.php";
?>
admin_content.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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.