【发布时间】:2014-07-15 04:06:15
【问题描述】:
我有一个奇怪的问题。我在根目录中有一个文件 index.php,在根目录中有两个文件 include 目录。
root > index.php , 包含
包括 > config.php , db.php
在 include/db.php 中我定义了 MySQL 连接参数,如下所示:
$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname = 'mydb';
然后我将这个文件包含在 include/config.php 中:
include 'db.php';
最后,我在 index.php 中包含了 include/config.php。
我遇到的问题是我在运行 index.php 时无法访问数据库变量。它们仅在我运行 include/config.php 时可用。但是我希望当我将它们包含在 index.php 中时,我必须在这两个文件中包含所有已定义的变量。
有什么问题?
【问题讨论】:
-
index.php包括config.php包括db.php— 对吗?另外,你怎么知道你无权访问变量?print_r(get_defined_vars());输出什么? -
真的是
include()不行吗?如果是这种情况,您应该会收到错误消息! -
您如何尝试将 config.php 包含在 index.php 中?语法?
-
@MohammadSaberi 将此添加到 index.php 的顶部,然后向我们显示错误
error_reporting(-1); -
我刚刚在本地测试过,效果很好。我只能假设您在某处犯了拼写错误。
标签: php variables include require