我寫了一個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();
密碼等是正確的,我也嘗試本地主機,但後來我得到一個錯誤他無法找到該文件,也許你可以幫助我。
更新問題與問題行分解 - 第24行 – dbmitch
請小心避免發佈密碼。 –
我更新了它,帶你@dbmitch –