简体   繁体   English

PHP define() 似乎不适用于 include()

[英]PHP define() doesn't seem to be working with include()

I've been trying my hand at OO PHP, and currently have three files.我一直在尝试 OO PHP,目前有三个文件。 I have a class_lib.php which, at the moment, just has a databaseServer class, an index.php file and a definitions.php file.我有一个 class_lib.php,目前只有一个 databaseServer 类、一个 index.php 文件和一个 definition.php 文件。 I want to put all my sensitive database info into the definitions file.我想将所有敏感的数据库信息放入定义文件中。 However, when I do, I get an error when trying to connect to the database: "Unkown server DB_HOST".但是,当我这样做时,在尝试连接到数据库时出现错误:“未知服务器 DB_HOST”。 My definitions file is:我的定义文件是:

<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","password");
define("DB_NAME","database");
?>

Then I use them in the index file like so:然后我在索引文件中使用它们,如下所示:

include('definitions.php');
include('class_lib.php');

$testing = new databaseServer();

$testing->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

And the function I use in the databaseServer class is this:我在 databaseServer 类中使用的函数是这样的:

    function connect($host,$user,$pw,$db) {
        $this->con = mysql_connect($host,$user,$pw);
        if (!$this->con) {
            die('Could not connect: ' . mysql_error());
            }
        $this->selectDb($db);
        }

    function selectDb($database) {
        $this->db = mysql_select_db($database,$this->con);
        if (!$this->db) {
            echo "Could not Select database: " . mysql_error();
            }
        }

Any ideas why this would not work?任何想法为什么这不起作用? I've also tried putting the definitions file into an include in the class_lib file, but it still doesn't work.我也试过将定义文件放入 class_lib 文件中的一个包含中,但它仍然不起作用。

This should work fine, and I've never seen it not work.这应该可以正常工作,而且我从未见过它不起作用。

  • Make 100% sure the includes are in the correct order (the defines need to be loaded first) 100% 确保包含的顺序正确(需要先加载定义)

  • Make test outputs of the constant values in the "definitions.php" file and the index file对“definitions.php”文件和索引文件中的常量值进行测试输出

  • Make 100% sure you are calling the right files确保 100% 确定您正在调用正确的文件

Please check if your server supports short_open_tag this value would be commented.请检查您的服务器是否支持 short_open_tag 此值将被注释。 I just enabled it with on and it started working.我刚刚启用它并开始工作。

working在职的

<?php
define('KLINGON_SEPARATOR', '');
?>

not working不工作

<?php
define('KLINGON_SEPARATOR', '');

silly... IDEA says "redundant closing tag"愚蠢的...... IDEA 说“冗余结束标签”

call it config.php称之为 config.php

if(!$link)
{

die('Failed to connect to server: ' . mysql_error());

}

$db = mysql_select_db(DB_DATABASE);
if(!$db)
{

die("Unable to select database");

}

?>

then include("config.php");然后包含("config.php"); on your pages在您的网页上

You just need to take out the define functions of the destinations file and transfer it to the source file (in your case "translation.php") and ready to work!您只需要取出目标文件的define函数并将其传输到源文件(在您的情况下为“translation.php”)并准备工作!

Like this the bug with define functions on receiving string params do not happen and you will include the CONSTANTS implemented already.像这样,接收字符串参数的定义函数的错误不会发生,您将包含已经实现的常量。

I faced this trouble and found myself just this one solution.我遇到了这个麻烦,发现自己只是这个解决方案。

I had a similar issue today and it seems to possibly be an invalid character code at the end of the file.我今天遇到了类似的问题,它似乎可能是文件末尾的无效字符代码。 I found this on stack PHP define is not registering constants His solution (removing extra spaces) worked for me.我在堆栈上发现这个PHP 定义没有注册常量他的解决方案(删除额外的空格)对我有用。

其中的关键是使用 constant() 函数从定义的常量中提取值:

echo constant("DB_USERNAME");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM