简体   繁体   English

未知的 MySQL 服务器主机 'DB_HOST' (0)

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

I am trying to sent data to the server database, i implement it using wamp server it was working fine but when i installed the db on my domain and switched it to my domain its giving the following error.我正在尝试将数据发送到服务器数据库,我使用 wamp 服务器实现它它工作正常但是当我在我的域上安装数据库并将其切换到我的域时,它给出了以下错误。

I have kept my files inside a folder in root directory我将文件保存在根目录的文件夹中

Unknown MySQL server host 'DB_HOST' (0)未知的 MySQL 服务器主机 'DB_HOST' (0)

Below is my config file i am using to connect to the db下面是我用来连接数据库的配置文件

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

here my code connecting db, config.php file contains the code pasted above这里我的代码连接数据库,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);
        }
}
?>

and my file receiving data和我的文件接收数据

<?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();
?>

I have seen related post but didn't find useful answers.我看过相关的帖子,但没有找到有用的答案。 Can anyone help me in getting out of this.任何人都可以帮助我摆脱这种情况。 Thanks in advance.提前致谢。

Please post the code where you try to connect to the server.请张贴您尝试连接到服务器的代码。 I guess you tried to connect using the following function:我猜您尝试使用以下功能进行连接:

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

Since you defined the host using define('DB_HOST', 'localhost');由于您使用define('DB_HOST', 'localhost');定义了主机define('DB_HOST', 'localhost'); , you do not have to use apostrophes. ,您不必使用撇号。 DB_HOST is a constant here. DB_HOST在这里是一个常量。 So try to use to所以尝试使用

mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);

instead.反而。 If you use apostrophes like in 'DB_HOST' , it is interpreted as a string instead of the value of the constant DB_HOST containing localhost .如果您在'DB_HOST'使用撇号,它会被解释为字符串,而不是包含localhost的常量DB_HOST的值。 That's a possible reason why you get the error that DB_HOST is an unknown host.这就是您收到DB_HOST是未知主机的错误的可能原因。

But please post the connection part of your code to see if that's really the mistake.但是请发布代码的连接部分,看看这是否真的是错误。

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

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