簡體   English   中英

奇怪的PHP要求/包含錯誤

[英]Weird php require/include error

我是一個相對較新的開發人員,但確實了解一些知識。

除了Lynda的基礎知識之外,我還在做有關PHP的教程,並試圖制作數據庫類。

在課堂上寫完所有內容后,我嘗試查看是否建立了連接。

require_once("../includes/database.php");
if(isset($database)) {
    echo "true<br />";
}else {
    echo "false<br />";
}

上面的代碼是我的測試,看是否還好

問題是當我嘗試使用“ require_once(” config.php“);”時 在數據庫類中。

require_once("config.php");//line 13

class MySQLDatabase{
   //code that is not important for this issue
}

這使我在頁面上出現以下錯誤:

 Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in       E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in      E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13
 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

數據庫連接失敗:php_network_getaddresses:getaddrinfo失敗:此類主機未知

如果我使用(下面的代碼)代替了require / require_once / include / include_once部分,它就可以工作!

define("DB_SERVER", "localhost");
define("DB_USER", "gallery");
define("DB_PASS", "123465");
define("DB_NAME", "photo_gallery");

這不是我第一次使用require / include ...,但是我真的很困惑為什么這種方法不起作用:-/(一旦我完成了DB類,我將把所有內容更改為mysqli,因為它是教程。不,我無權訪問練習文件)

(對不起,很長的帖子)

聽起來在config.php中,定義常量時缺少引號。

wrong:
define(DB_SERVER, "localhost");
right:
define("DB_SERVER", "localhost");

仔細檢查一下,或從config.php中發布相關部分:)

嘗試通過以下方式定義常量。

defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "gallery");
defined('DB_PASS')   ? null : define("DB_PASS", "123456");
defined('DB_NAME')   ? null : define("DB_NAME", "photo_gallery");

除去Notice: Use of undefined constant DB_SERVER通知。

看起來我們丟失了什么或者您的mysql數據庫已關閉?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM