0

我有几个文件,包括如下:

索引.php

require("database.php");
require("header.php");
require("template.php");
require("footer.php");

数据库.php

require("constants.php");
class connect{
 public function connect(){
 mysql_connect(LOCALHOST,USER,PASS);
}
}

常量.php

define('LOCALHOST','localhost');
define("USER","username");
define("PASS","password");
define("PRICE1","10.00");
define("PRICE2","11.00");
define("PRICE3","12.00");
define("PRICE4","13.00");

模板.php

*html code*
*ajax code*
*ajax output*

** 它是输出错误 load.php (ajax get) 的 ajax 负载

require(database.php);
then html code

输出错误:

注意:使用未定义的常量 PRICE1 - 在第 34 行的 web/site/files/template.php 中假定为“PRICE1”

任何人都得到了任何建议,因为我被困在为什么没有像我定义的那样调用常量

让我澄清一下问题:

常量包含在 database.php 中,database.php 使用常量进行连接并且工作正常。当 template.php 需要 database.php 时,会显示错误。问题是为什么当常量在 database.php 上工作时它不传递常量


如果我运行 load.php 错误消息仍然存在,但我再次在那里运行数据库,该数据库正在运行,但无法从 load.php 调用任何常量

查看完整的 load.php 代码:

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
if(isset($_GET['label']) && $_GET['type'] == "yes" && $_GET['dif'] == "label"){

    try{
        require_once("../classes/database.php");
        $db = new MySQL();
        $connection = $db->connect();
        $sql = "SELECT * FROM `label` WHERE id=?";
        $result = $db->prepare($sql,array($_GET['label']),$connection);
        if(!$result){
            throw new Exception("Error: ");
        }
        $number = $result['personalisedlines'];

        for($i=0;$i<$number;$i++){
            echo "<label>Line ".($i+1)."</label>\n\r<br />\n\r";
            echo "<input type='hidden' name='label_price' value='".PRICE1."' />";
            echo "<input type='text' name='field".$i."' />\n\r<br />\n\r";
        }
        }   catch(Exception $e){
            throw $e;
        }
}
if($_GET['type'] == "yes" && $_GET['dif'] == "engraving"){
    echo "<br />\n\r<label>Engraved Message</label>\n\r<br />\n\r";
    echo "<input type='hidden' name='engraving_price' value='".PRICE2."' />";
    echo "<textarea cols='30' rows='10' name='personalisedbox' row='10'></textarea>\n\r<br />\n\r";
}

if($_GET['type'] == "yes" && $_GET['dif'] == "printed"){
    echo "<br />\n\r<label>Printed Message</label>\n\r<br />\n\r";
    echo "<input type='hidden' name='printed_price' value='".PRICE3."' />";
    echo "<textarea cols='30' rows='10' name='personalisedbox' row='10'></textarea>\n\r<br />\n\r";
}

if($_GET['type'] == "yes" && $_GET['dif'] == "flutes"){
    echo "<br />\n\r<label>Engraved Message</label>\n\r<br />\n\r";
    echo "<input type='hidden' name='engraving_price' value='".PRICE4."' />";
    echo "<textarea cols='30' rows='10' name='personalisedflutes' row='10'></textarea>\n\r<br />\n\r";
}

?>
4

2 回答 2

1

我还不知道为什么 require 在正确的范围内没有流行起来。也许您提出问题的方式与您的情况并不完全相同?请显示更多代码。

但是你可以做的是:

换成... require_require_once

现在将该require_once()行复制/粘贴到您希望使用常量的每个 php 文件中。PHP 将确保 require 只执行一次,因此您不会在重新定义常量时遇到任何错误。

于 2013-06-06T13:47:15.767 回答
0

包括constants.phptemplates.php.

于 2013-06-06T13:40:11.550 回答