簡體   English   中英

未知的 MySQL 服務器主機 'DB_HOST' (0)

[英]Unknown MySQL server host 'DB_HOST' (0)

我正在嘗試將數據發送到服務器數據庫,我使用 wamp 服務器實現它它工作正常但是當我在我的域上安裝數據庫並將其切換到我的域時,它給出了以下錯誤。

我將文件保存在根目錄的文件夾中

未知的 MySQL 服務器主機 'DB_HOST' (0)

下面是我用來連接數據庫的配置文件

<?php
 // Database configuration
    define('DB_USERNAME', 'user');
    define('DB_PASSWORD', 'xxxx');
    define('DB_HOST', '192.210.195.245');
    define('DB_NAME', 'tabanico_userlogin');
?>

這里我的代碼連接數據庫,config.php 文件包含上面粘貼的代碼

<?php
class DbConnect {  
        private $conn;        
        function __construct() {        
        // connecting to database
        $this->connect();
        }        
        function __destruct() {        
        $this->close();
        }        
        function connect() {        
        include_once dirname(__FILE__) . './Config.php';                  
        $this->conn = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die(mysql_error());         
        mysql_select_db(DB_NAME) or die(mysql_error());        
        // returing connection resource
        return $this->conn;
        }        
         // Close function          
        function close() {
        // close db connection
        mysql_close($this->conn);
        }
}
?>

和我的文件接收數據

<?php
include_once './DbConnect.php';
function createNewPrediction() {

        $response = array();
        $id = $_POST["id"];
        $name = $_POST["name"];
        $email = $_POST["email"];
    $password = $_POST["password"];

        $db = new DbConnect();
       // mysql query
        $query = "INSERT INTO login(id,name,email,password) VALUES('$id','$name','$email','$password')";
        $result = mysql_query($query) or die(mysql_error());
        if ($result) {
            $response["error"] = false;
            $response["message"] = "Prediction added successfully!";
        } else {
            $response["error"] = true;
            $response["message"] = "Failed to add prediction!";
        }
       // echo json response
    echo json_encode($response);
}
createNewPrediction();
?>

我看過相關的帖子,但沒有找到有用的答案。 任何人都可以幫助我擺脫這種情況。 提前致謝。

請張貼您嘗試連接到服務器的代碼。 我猜您嘗試使用以下功能進行連接:

mysql_connect('DB_HOST', 'DB_USERNAME', 'DB_PASSWORD');

由於您使用define('DB_HOST', 'localhost');定義了主機define('DB_HOST', 'localhost'); ,您不必使用撇號。 DB_HOST在這里是一個常量。 所以嘗試使用

mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);

反而。 如果您在'DB_HOST'使用撇號,它會被解釋為字符串,而不是包含localhost的常量DB_HOST的值。 這就是您收到DB_HOST是未知主機的錯誤的可能原因。

但是請發布代碼的連接部分,看看這是否真的是錯誤。

暫無
暫無

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

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