当我尝试使用php连接到我的数据库时500内部服务器错误

时间:2019-05-02 21:19:40

标签: php mysql mysqli

我正在尝试连接到外部服务器(数字海洋)上的数据库,我的Web服务在php中。但是每次我调用连接到数据库的php时,都会出现内部伺服错误500。

它经过简化,因此我无法找到问题所在,并且从中得知它从未连接到数据库。但是,由于我得到的只是该错误,因此我似乎无法找到其他问题。我尝试更改密码以防万一,但仍然无法使用。

db_config.php

define('DB_SERVER', "165.227.37.36"); // serveur
define('DB_USER', "meandfun");  //username
define('DB_PASSWORD', ""); ///pwd
define('DB_DATABASE', "paw_finder"); //db

db_connect.php

class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

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

    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';

        // Connecting to mysql database
        $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());

        // Selecing database
        $db = mysqli_select_db(DB_DATABASE) or die(mysql_error()) or die(mysqli_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysqli_close();
    }

}

insert_user.php

$response = array();

require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();


$result = mysql_query("INSERT INTO usager(username, pwd, email) VALUES('hghfgh', 'fgdhdfga', 'dfhgdgfh')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = true;
    $response["message"] = "Product successfully created.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = false;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}

0 个答案:

没有答案
相关问题