我写了一个php脚本,它应该将我与我的数据库连接起来。我还将文件上传到了public_html部分,但它总是抛出这个错误:
警告:mysqli :: mysqli():( HY000 / 2002):第24行/Applications/XAMPP/xamppfiles/htdocs/FoodHelperSwift/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();
密码等是正确的,我也试过localhost,但后来我得到一个错误,他找不到文件,也许你可以帮助我。
答案 0 :(得分: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 :(得分: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主机名。