2016-08-18 63 views
1

我寫了一個PHP腳本應該連接我與我的數據庫。我也上傳了public_html部分的文件,但它總是拋出這個錯誤:無法連接到我的Bluehost SQLDatabase與PHP腳本

Warning:mysqli :: mysqli():(HY000/2002):連接在/ Applications/XAMPP/xamppfiles/htdocs/FoodHelperSwift /上線24

這裏DB/public_html.php是行:$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);

這是我的代碼:

的public_html

var $conn = null; 
    var $result = null; 
    public $dbhost = null; 

    public $dbname = null; 
    public $dbuser = null;  
    public $dbpass = null; 

    function __construct($dbhost, $dbuser, $dbpassword, $dbname) { 
     $this->dbhost = $dbhost; 
     $this->dbuser = $dbuser; 
     $this->dbpass = $dbpassword; 
     $this->dbname = $dbname; 
    } 


    public function openConnection() 
    { 
     $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); 

     echo "YESSSS"; 
     echo $this->dbhost; 
     echo $this->dbuser; 
     echo $this->dbpass; 
     echo $this->dbname; 
     if(mysqli_connect_errno()) 
     { 

      throw new Exception("Could not connect with database"); 
      $this->conn->set_charset("utf8"); 
     } 
    } 

    public function closeConnection() 
    { 
     if ($this->conn != null) 
     { 
      $this->conn->close(); 
     } 
    } 

} 

註冊用戶

require("../db/public_html.php");  
    $dbhost = "127.0.0.1"; 
    $dbname = "xxxxxxxx"; 
    $dbuser = "xxxxxxxxx";  
    $dbpassword = "xxxxxxx";  

$returnValue = array(); 

if(empty($_REQUEST["userEmail"]) || empty($_REQUEST["userPassword"]) 

     || empty($_REQUEST["userFirstName"]) 

     || empty($_REQUEST["userLastName"])) 
{ 
    $returnValue["status"]="400"; 

    $returnValue["message"]="Missing required information"; 

    echo json_encode($returnValue); 

    return; 
} 

$userEmail = htmlentities($_REQUEST["userEmail"]);  
$userPassword = htmlentities($_REQUEST["userPassword"]);  
$userFirstName = htmlentities($_REQUEST["userFirstName"]);  
$userLastName = htmlentities($_REQUEST["userLastName"]);  
$salt = openssl_random_pseudo_bytes(16);  
$secured_password = sha1($userPassword . $salt);  
$dao = new MySQLDAO($dbhost, $dbuser, $dbpassword, $dbname);  
$dao->openConnection(); 

密碼等是正確的,我也嘗試本地主機,但後來我得到一個錯誤他無法找到該文件,也許你可以幫助我。

+0

更新問題與問題行分解 - 第24行 – dbmitch

+0

請小心避免發佈密碼。 –

+0

我更新了它,帶你@dbmitch –

回答

2

最有可能你沒有按照你的網站託管服務提供商提供的步驟。 第三方託管解決方案通常要求您設置您的遠程IP

從您指定的錯誤消息看來,您嘗試從您自己的家中XAMP Web服務執行此操作。

首先 - 基本

localhost - 不在家工作 - 因爲這是要去尋找自己的MySQL數據庫不託管分貝

第二 - 從家裏(或遠程)登錄
閱讀文檔(總是一個好主意)Remote access to Bluehost MySQL

使用下面的配置設置連接到數據庫

Host name = (use the server IP address) 
Database name = (cpanelUsername_databaseName) 
Database username = (cpanelUsername_databaseUsername) 
Database password = (the password you entered for that database user) 
MySQL Connection Port = 3306 
TCP or UDP, either is fine. 

允許遠程服務器訪問數據庫

從另一臺計算機連接到MySQL之前,連接計算機必須作爲訪問主機啓用。

Log into cPanel and click the Remote MySQL icon, under Databases. 
Type in the connecting IP address, and click the Add Host button. 
    Note: You can find and add your IP address directly from this tool. Look for Your IP is: 123.123.12.123 [Add]. Clicking the [Add] link will input your IP into the field box below. 
Click Add, and you should now be able to connect remotely to your database. 
1

要解決這個問題,請將代碼剝離到基礎。讓自己一點點testMyDb.php文件,只包含最少的東西。例如:

$dbhost = "127.0.0.1"; 
$dbname = "xxxxxxxx"; 
$dbuser = "xxxxxxxxx"; 
$dbpassword = "xxxxxxx"; 
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname); 
if ($conn->connect_errno) { 
    printf("Connect failed: %s\n", $mysqli->connect_error); 
    exit(); 
} 

一旦你的工作,你可以繼續調試你的PHP類,並確保你正確設置的東西了。

你的名爲public_html.php文件包含了一些代碼,這是一個php class implementation(例如__construct())的一部分,但我沒有看到一個class ClassName {線設立一個類定義。你可能從某個地方複製了一些代碼片段而沒有全部完成。

如果您的簡單測試不起作用,請使用bluehost的技術支持krewe進行檢查。您可能需要一些特殊憑據或數據庫名稱才能從其Windows主機之一連接到MySQL。

如果您在一臺bluehost機器上使用MySQL服務器,並嘗試從本地機器連接到該服務器,那麼這將不起作用(尤其是127.0.0.1)。您需要配置bluehost以允許遠程MySQL連接,並且您必須使用實際的MySQL主機名。