简体   繁体   中英

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.

I have kept my files inside a folder in root directory

Unknown MySQL server host '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

<?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'); , you do not have to use apostrophes. DB_HOST is a constant here. 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 . That's a possible reason why you get the error that DB_HOST is an unknown host.

But please post the connection part of your code to see if that's really the mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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