【问题标题】:why does not PHP include work?为什么 PHP 不包含工作?
【发布时间】: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


【解决方案1】:

如果您在 index.php 中包含了 config.php,在 config.php 中包含了 db.php。那么 index.php 应该是:include 'include/config.php'; 和 config.php 应该是:include 'include/db.php';

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多