douzhuo6931 2015-10-31 16:51
浏览 316

未知的MySQL服务器主机'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.

  • 写回答

1条回答 默认 最新

  • duanhan7001 2015-10-31 20:09
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私