2016-08-18 56 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主机名。