2014-08-28 95 views
1

我试图登录到我的应用程序,这是我得到的错误:应用程序崩溃后,我尝试登录

08-28 07:57:31.450: E/JSON(1047): <br />n<b>Warning</b>: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. 
in <b>C:\xampp\htdocs\API\include\DB_connect.php</b> on line <b>19</b><br />n<br />n<b>Warning</b>: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. 
in <b>C:\xampp\htdocs\API\include\DB_connect.php</b> on line <b>19</b><br />nNo database selectedn 
08-28 07:57:31.450: E/JSON Parser(1047): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 

它看起来像它没有连接到数据库成功,但是我不”不明白为什么。

此外,这是我的index.php文件,该文件位于xampp/htdocs/api

​​

这是config.php文件:

<?php 

    define("DB_HOST", "localhost"); 
    define("DB_USER", "root"); 
    define("DB_PASSWORD", ""); 
    define("DB_DATABASE", "dbapp"); 
?> 

db_connect.php文件:

<?php 

class DB_Connect { 

    // constructor 
    function __construct() { 

    } 

    // destructor 
    function __destruct() { 
     // $this->close(); 
    } 

    // Connecting to database 
    public function connect() { 
     require_once 'config.php'; 
     // connecting to mysql 
     $con = mysql_connect('DB_HOST', 'DB_USER', 'DB_PASSWORD'); 
     // selecting database 
     mysql_select_db('DB_DATABASE'); 

     // return database handler 
     return $con; 
    } 

    // Closing database connection 
    public function close() { 
     mysql_close(); 
    } 

} 

?> 

回答

0

我发现其原因在于DB_conne CT文件,我应该插入的参数,而不是直接在首先从config.php文件

得到它是:

public function connect() { 
    require_once 'config.php'; 
    // connecting to mysql 
    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
    // selecting database 
    mysql_select_db(DB_DATABASE); 

    // return database handler 
    return $con; 
} 

其应该是:

public function connect() { 
    require_once 'config.php'; 
    // connecting to mysql 
    $con = mysql_connect('127.0.0.1', 'root', ''); 
    // selecting database 
    mysql_select_db('dbapp'); 

    // return database handler 
    return $con; 
} 
+0

这是错误的,你只需要使用'require'导入文件'config.php'并在你的文件 – Sal00m 2014-08-28 09:18:41

1

几个问题:

  1. 摆脱mysql_ *函数,它们被弃用,没有多久呃维护,使用的mysqli或PDO扩展
  2. 您使用常量字符串,这样的mysql_connect无法连接到名为任何服务器DB_HOST
  3. 您未导入config.php文件,所以db_connect.php不知道有关的常数

所以,db_connect.php会是这样的(我将使用* mysql_功能,但你至少应该使用mysqli的功能,我可以重写使用PDO类如果需要的话)

<?php 

require('config.php); // <--- Added require 

class DB_Connect { 

    // constructor 
    function __construct() { 

    } 

    // destructor 
    function __destruct() { 
     // $this->close(); 
    } 

    // Connecting to database 
    public function connect() { 
     require_once 'config.php'; 
     // connecting to mysql 
     $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); //<---- REMOVED ' 
     // selecting database 
     mysql_select_db(DB_DATABASE); 

     // return database handler 
     return $con; 
    } 

    // Closing database connection 
    public function close() { 
     mysql_close(); 
    } 

} 

?> 
+0

上使用它,当我尝试添加require('config.php);我的所有屏幕都变灰了,你确定代码是正确的? – 2014-08-28 11:44:37

+0

这取决于你的config.php是。我写的代码认为'config.php'和db_connect.php是在同一个目录下,如果不是只改变路径。和tur php的错误(你所描述的错误给我一个致命的错误,我猜) – Sal00m 2014-08-28 12:07:37

相关问题