Paul Heinemeyer Paul Heinemeyer - 3 months ago 17
MySQL Question

Cant connect to my Bluehost SQLDatabase with php script

i wrote a php script which should connect me with my database. I also uploaded the files in the public_html section, but it always throws this error :

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in /Applications/XAMPP/xamppfiles/htdocs/FoodHelperSwift/db/public_html.php on line 24

Here is the line :

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


This is my code :


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

}


and


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


The password etc. is right and i also tried localhost, but then i got an error that he cant find the file maybe you could help me.

Answer

Most likely you're not following steps provided by your website hosting provider. Third party hosting solutions usually require you setup your remote IP

From the error message you specified it looks like you're trying to do this from your own home XAMP web service.

First - the basics

: localhost - won't work from home - because that's going to look for your own MySQL database not the hosted db

Second - logging in from home (or remotely)
read the docs (always a good idea) Remote access to Bluehost MySQL

Use the following configuration settings for connecting to your database

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.

Allowing a Remote Server to Access Your Database

Before connecting to MySQL from another computer, the connecting computer must be enabled as an Access Host.

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.
Comments