gpt4 book ai didi

php - 无法使用 php 脚本连接到我的 Bluehost SQL 数据库

转载 作者:行者123 更新时间:2023-11-29 05:11:18 25 4
gpt4 key购买 nike

我写了一个 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();
}
}

}

register user

:

    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();

密码等都是正确的,我也试过本地主机,但后来我得到一个错误,他找不到文件也许你可以帮助我。

最佳答案

很可能您没有按照您的网站托管服务提供商提供的步骤进行操作。第三方托管解决方案通常需要您设置远程 IP

从您指定的错误消息来看,您似乎正试图通过您自己的家庭 XAMP 网络服务执行此操作。

First - the basics

:localhost - 无法在家工作 - 因为那将查找您自己的 MySQL 数据库而不是托管数据库

Second - logging in from home (or remotely)
read the docs (always a good idea) 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.

关于php - 无法使用 php 脚本连接到我的 Bluehost SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com