PHP include() does not load config.php


PHP include() does not load config.php

我试图制作一个简单的脚本,但当我试图包含配置文件时,它无法加载。这是我的代码-

<?php
$config = include("http://MyWebSite/config.php");
$mysql = mysql_connect($host, $user, $pass);
if (!$mysql) { die(mysql_error()); }
$data = mysql_select_db($database);
if (!$data) { die(mysql_error()); }
?>

试试这个:

include($_SERVER['DOCUMENT_ROOT'] . '/config.php');

此外,不要将其分配给变量。您只想在脚本中包含该文件。

1)include不返回任何内容2) 包含整个url必须由服务器启用

你应该试试

<?php
include("config.php");
?>

因为在正常的服务器设置中,不允许直接远程包含文件。

您需要确保文件的路径是正确的。以下代码将在与您正在运行的脚本相同的目录中查找config.php:

include('config.php');

如果这不起作用,您可以查看错误日志或使用以下代码打开错误报告:

ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
include('config.php');

错误消息现在应该会显示它在哪里查找文件。错误看起来如下:

Warning: include() [function.include]: Failed opening 'config.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /path/to/your/script.php on line 4

上面的消息表示在以下任何位置都找不到config.php:

 /path/to/your/ 
 /usr/share/pear/
 /usr/share/php/