我知道这个问题已经被问了很多次了,我确实经历了大部分,但似乎没有一个解决方案对我有效。我必须主要包括三个文件,它们都包含在includes/connectdb、functions和config中。在配置中,我使用了define(dir_somefile,'includes/filename'),还使用了includes/function.php中的一些函数。但它总是给我一个错误,说未定义的变量或地址。如果有人想要的话,我会附上主页、配置、连接和函数的代码。
主页:
<?php
include("includes/config.php");
include(DIR_DB_CONNECT_SITE);
include(DIR_DB_FUNCTION_SITE);
$sqlabout="select aboutus_id,title,content from ".TABLE_ABOUTUS." order by aboutus_id";
$resultabout=exec_query($sqlabout,"Get About Details");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
config.php:
$runon=1;
date_default_timezone_set("Asia/Calcutta");
$DBHOST="localhost";
$DBNAME="brainsan_mjmp";
$DBUSER="root";
$DBPASSWORD="";
define('DIRPATH',$_SERVER['DOCUMENT_ROOT']."/mjmp/property_images");
define('THUMB_PATH',$_SERVER['DOCUMENT_ROOT']."/mjmp/property_images/thumbnail");
//define('DIRPATH',$_SERVER['DOCUMENT_ROOT']."/mjmp/member_images");
// define the directories details used in Front section
define('DIR_DB_CONNECT_SITE', 'includes/connect_db.php'); // for db connection
define('DIR_DB_FUNCTION_SITE', 'includes/function.php'); // for reusable functions
define('DIR_DB_CLASSES_SITE', 'includes/classes.php'); // (file/image) upload class
define('DIR_DB_SECURE_SITE', 'includes/secure.php'); // secure login file
define('DIR_DB_MEMBER_SECURE_SITE', 'includes/member-secure.php'); // secure login file
define('DIR_MEMBER_IMAGES_SITE','member_images/'); // uploaded member images
define('DIR_PROPERTY_IMAGES_SITE','property_images/'); // uploaded property images
define('DIR_THUMB_IMAGES_SITE','property_images/thumbnail/'); // uploaded images home dir path
define('DIR_ADS_SITE','advt/'); // uploaded advertisement images
connect_db.php:
<?php
$link = mysql_connect($DBHOST,$DBUSER,$DBPASSWORD) or die("<b>DataBase Connection Error!</b>");
$connection = mysql_select_db($DBNAME,$link) or die("<b> DataBase Select Error!</b>".mysql_error());
?>
function.php:
<?
//function to execute a query
function exec_query($sql,$title)
{
$result=mysql_query($sql);
if($result)
{
return $result;
}
else
{
echo "Error in <b>$title</b> <br>".$sql."<br>". mysql_error();
exit();
}
}
这些文件要大得多,但我只添加了我认为相关的部分。如果需要更多,请告诉我。文件:主页.php包括/connect_db.php、function.php、confg.php
错误:
使用未定义的常量DIR_DB_CONNECT_SITE-假定为"DIR_DB_COMPONT_SITE"
警告:include(DIR_DB_CONNECT_SITE):无法打开流:没有这样的文件或目录
致命错误:调用未定义的函数exec_query()
这在PHP中可能会令人困惑,因为很少有人真正了解include_path的工作原理。
我发现一个适合很多人的解决方案是修复他们的包含路径,以便PHP搜索他们项目的基本目录。
config/config.php
/* fix up include paths */
$include_path = ini_get('include_path');
$include_path = sprintf("%s%s%s", $include_path, PATH_SEPARATOR, realpath(__DIR__ . '/../'));
ini_set('include_path', $include_path);
$include_path = ini_get('include_path');
define('APPROOT', realpath(__DIR__ . '/../'));
index.php
require_once("config/config.php"); // once this is called, you can use relative paths
require_once("config/database.php"); // like this
require_once(APPROOT . "/config/memcached.php"); // or use APPROOT like this
深度/嵌套/索引.php
require_once("../../config/config.php"); // once this is called, you can use relative paths
require_once("config/database.php"); // like this
require_once(APPROOT . "/config/memcached.php"); // or use APPROOT like this