absentx
absentx

Reputation: 1417

PHP mysqli connection to remote database

Trying to connect to a database that is not on the same host as my site for the first time and not having much luck. The DB is on a godaddy host and I have configured it to allow remote access:

$dbc = mysqli_connect($db_host,$db_user,$db_pass,$db_name);

if (!$dbc) {
    die('Connect Error: ' . mysqli_connect_error());
}

and I get the following on page:

Warning: mysqli_connect() [function.mysqli-connect]: [2002] Connection timed out (trying to connect via ‘mydbhostname:3306) in ‘path’ on line 3

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Connection timed out in ‘path’ on line 3

Connect Error: Connection timed out

Anything I need to look for on my end or something in the script that could be causing this?

EDIT: well it appears everything works when I use mysql, but not mysqli...and mysqli is enabled on the server.

Upvotes: 0

Views: 12129

Answers (3)

Kurt Van den Branden
Kurt Van den Branden

Reputation: 12933

I had the same problem on a Digitalocean Ubuntu 14.04 server, where the firewall configuration was the culprit. Mysql wasn't allowed on port 3306 by the ufw firewall rules.

Solution:

$ sudo ufw status
$ sudo ufw allow mysql
$ sudo ufw reload

Upvotes: 1

Jason
Jason

Reputation: 7

I don't understand the difference between mysqli and mysql, but I've found on GoDaddy that using:

mysqli_connect doesn't work. mysql_connect does work.

Try using the following sample code to see if you can connect to your database which needs data in at least one table:

<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "XXXXXXX.db.XXXXXXXX.hostedresource.com";
$username = "XXXXXXX";
$dbname = "XXXXXXX";

//These variable values need to be changed by you before deploying
$password = "XXXXXXX";
$usertable = "XXXXXXX";
$yourfield = "XXXXXXX";

//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);

//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);

if ($result) {
    while($row = mysql_fetch_array($result)) {
        $name = $row["$yourfield"];
        echo "<h2>some data</h2>";
        echo "$name<br>";
    }
}
?>

Upvotes: -2

SubRed
SubRed

Reputation: 3187

Make sure that you have inserted correct hostname for your database, here you can see how to do it on godaddy hosting:

Upvotes: 0

Related Questions