2016-02-28 318 views
0

我正在使用以下代碼連接到一個虛擬主機服務器上的sql數據庫。 我對着錯誤PHP登錄數據庫連接錯誤

警告:mysqli的:: mysqli的()(HY000/2002):無法通過套接字 '/var/run/mysqld/mysqld.sock' 連接到本地MySQL服務器( 2)在/srv/disk7/2067595/www/envoycc.atwebpages.com/DB_Connect.php第10行

第10行是$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

我的總代碼是

的config.php

<?php 

/** 
* Database config variables 
*/ 
define("DB_HOST", "localhost"); 
define("DB_USER", "2067595_android"); 
define("DB_PASSWORD", "password"); 
define("DB_DATABASE", "2067595_android"); 
?> 

DB_Connect.php

<?php 
class DB_Connect { 
private $conn; 

// Connecting to database 
public function connect() { 
    require_once 'config.php'; 

    // Connecting to mysql database 
    $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); 

    // return database handler 
    return $this->conn; 
} 
} 

?> 

DB_Functions.php

<?php 

class DB_Functions { 

    private $conn; 

    // constructor 
    function __construct() { 
     require_once 'DB_Connect.php'; 
     // connecting to database 
     $db = new Db_Connect(); 
     $this->conn = $db->connect(); 
    } 

    // destructor 
    function __destruct() { 

    } 

    /** 
    * Storing new user 
    * returns user details 
    */ 
    public function storeUser($name, $email, $password) { 
     $uuid = uniqid('', true); 
     $hash = $this->hashSSHA($password); 
     $encrypted_password = $hash["encrypted"]; // encrypted password 
     $salt = $hash["salt"]; // salt 

     $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())"); 
     $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt); 
     $result = $stmt->execute(); 
     $stmt->close(); 

     // check for successful store 
     if ($result) { 
      $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?"); 
      $stmt->bind_param("s", $email); 
      $stmt->execute(); 
      $user = $stmt->get_result()->fetch_assoc(); 
      $stmt->close(); 

      return $user; 
     } else { 
      return false; 
     } 
    } 

    /** 
    * Get user by email and password 
    */ 
    public function getUserByEmailAndPassword($email, $password) { 

     $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?"); 

     $stmt->bind_param("s", $email); 

     if ($stmt->execute()) { 
      $user = $stmt->get_result()->fetch_assoc(); 
      $stmt->close(); 
      return $user; 
     } else { 
      return NULL; 
     } 
    } 

    /** 
    * Check user is existed or not 
    */ 
    public function isUserExisted($email) { 
     $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?"); 

     $stmt->bind_param("s", $email); 

     $stmt->execute(); 

     $stmt->store_result(); 

     if ($stmt->num_rows > 0) { 
      // user existed 
      $stmt->close(); 
      return true; 
     } else { 
      // user not existed 
      $stmt->close(); 
      return false; 
     } 
    } 

    /** 
    * Encrypting password 
    * @param password 
    * returns salt and encrypted password 
    */ 
    public function hashSSHA($password) { 

     $salt = sha1(rand()); 
     $salt = substr($salt, 0, 10); 
     $encrypted = base64_encode(sha1($password . $salt, true) . $salt); 
     $hash = array("salt" => $salt, "encrypted" => $encrypted); 
     return $hash; 
    } 

    /** 
    * Decrypting password 
    * @param salt, password 
    * returns hash string 
    */ 
    public function checkhashSSHA($salt, $password) { 

     $hash = base64_encode(sha1($password . $salt, true) . $salt); 

     return $hash; 
    } 

} 

?> 

的login.php

<?php 
require_once 'DB_Functions.php'; 
$db = new DB_Functions(); 

// json response array 
$response = array("error" => FALSE); 

if (isset($_POST['email']) && isset($_POST['password'])) { 

    // receiving the post params 
    $email = $_POST['email']; 
    $password = $_POST['password']; 

    // get the user by email and password 
    $user = $db->getUserByEmailAndPassword($email, $password); 

    if ($user != false) { 
     // use is found 
     $response["error"] = FALSE; 
     $response["uid"] = $user["unique_id"]; 
     $response["user"]["name"] = $user["name"]; 
     $response["user"]["email"] = $user["email"]; 
     $response["user"]["created_at"] = $user["created_at"]; 
     $response["user"]["updated_at"] = $user["updated_at"]; 
     echo json_encode($response); 
    } else { 
     // user is not found with the credentials 
     $response["error"] = TRUE; 
     $response["error_msg"] = "Login credentials are wrong. Please try again!"; 
     echo json_encode($response); 
    } 
} else { 
    // required post params is missing 
    $response["error"] = TRUE; 
    $response["error_msg"] = "Required parameters email or password is missing!"; 
    echo json_encode($response); 
} 
?> 
+0

當您將主機設置爲「localhost」時,MySQL會嘗試使用UNIX套接字文件連接到指定的主機,在您的情況下,它似乎不可用。將DB_HOST常量設置爲127.0.0.1應該是你的一天。 –

+0

仍然有相同的錯誤。我有一個遠程虛擬主機服務器上的這個數據庫和PHP文件。我認爲localhost和127.0.0.1的意思是相同的東西127.0.0.1是由DNS生成的localhost的IP –

+1

您需要發佈您用來查詢的代碼,因爲在測試中您發佈的內容不會產生任何錯誤。另外,看到您使用的是AwardSpace,您需要將'localhost'更改爲他們的服務器'???。awardspace.net'。他們可能不允許遠程連接免費託管,如果這是你註冊的。 –

回答

2

正如我在評論中所指出:

AwardSpace送你一個電子郵件與服務器地址用於數據庫工作。

正如我所指出的???將被替換爲fdbxxx(xxx是數字),他們給你使用fdbxxx.awardspace.net

  • 這個我知道,因爲我也用他們的服務。