PHP mysqli connection to remote database - php

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.

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

Make sure that you have inserted correct hostname for your database, here you can see how to do it on godaddy hosting:
http://support.godaddy.com/help/article/4978/connecting-remotely-to-shared-hosting-databases
http://support.godaddy.com/help/article/39/locating-your-databases-host-name?locale=en

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>";
}
}
?>

Related

mysqli::__construct(): (HY000/2003): Can't connect to MySQL server on '127.0.0.1'

I've followed the W3Schools steps to connect to MySQL (https://www.w3schools.com/php/php_mysql_connect.asp) but I did not get it to work. Instead of working, I received the error: "mysqli::__construct(): (HY000/2003): Can't connect to MySQL server on '127.0.0.1'".
My dbcon.php (connect to database) code:
<?php
$server = "127.0.0.1";
$user = "root";
$password = "root";
$conn=new mysqli($server,$user,$password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_close($conn);
?>
I've also tried using "localhost" as server, my real username and password, blank password but all yield negative results. I came across several other stackoverflow posts about the same issue, but I could not resolve mine. In one post, the answer was setting your port to 3306, but my port is already 3306.
What is the problem and how can I fix it? Thanks in advance!
Turns out I did not use the right username. Apparently, you have to use the database name, not your Awardspace username.

Connect PHP on Linux server to MS SQL on remote server running Windows

Im having real trouble getting this to work. I have got the mssql-connect drivers set up and working on the Linux server, it now understands that function and does not return any errors.
I have had our server management team configure the windows server to allow the connection from the linux server. They have tested this and confirm it is all working, however I just cant seem to connect to it with PHP.
I have tried various connection strings, but it won't connect, here is an example
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '214.133.182.71,1443';
// Connect to MSSQL
$link = mssql_connect($server, '****', '******');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
I always get:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 214.133.182.71,1443 in /home/v3rec/public_html/test.php on line 10
Something went wrong while connecting to MSSQL
The username and password of that for the MS SQL database. Ive tried it with and without the port name. I am running SQL 2012, im not sure what the instance name would be? Am I missing something? Why is PHP unable to connect?
May I suggest you use PDO and do this:
$dbhost = "myhost";
$dbport = 10060;
$dbname = "tempdb";
$dbuser = "dbuser";
$dbpass = "password";
try {
$dbh = new PDO ("dlib:host=$dbhost:$dbport;dbname=$dbname","$dbuser","$dbpass");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}

How to get mysqli_connect to work?

I am trying to use mysqli_connect as such:
$host = "ftp.tatiana-renae.com";
$user = "tatiana";
$password = "********";
$dbname = "********";
$connection = mysqli_connect($host, $user, $password, $dbname);
but I get this error:
Database connection failed: Can't connect to MySQL server on 'ftp.tatiana-renae.com' (111) (2003)
I know mysqli_connect works differently than mysql_connect but I can't figure out what to do for mysqli_connect to make it happy. Any ideas?
Error 111 is "connection refused". One candidate explanation is that MySQL is not "listening" on the network, but only the TCP loopback address.
Check the my.cnf for the server, and check if it contains lines like this:
skip-networking
or
bind_address=127.0.0.1
If your PHP is running on the same server as MySQL, you can try using '127.0.0.1' for the host, in place of 'ftp.tatiana-renae.com'.
Is the MySQL server running? Can you connect to it using the mysql command line interface?
There's several other possibilities; but there's not enough information provided about you environment for us to accurately diagnose the problem. We're just guessing.

Unable to connect to MSSQL database in a different domain

HI friends I wants to connect local server (linux) to a remote desktop's mssql database. I have reviewed the php document for assistance.
It defines the use of mysql_connect().
I used that and ends with some issues mainly do to the difference in domain.
Here is the datas I have.
$dbname = 'dbname';
$servername = 'servername.abc.cbd.net\instancename';
$username = 'domain\username';
$password = 'password';
$port = '1433';
Here is the connection code I have used.
$link= mssql_connect($servername, $username, $password);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
Error am getting is
Warning: mssql_connect(): Unable to connect to server:servername.abc.cbd.net\instancename
Please help me to correct the code. Is there any other alternative for myssql_connect().
Thanks in advance.

Up until this morning my mysqli_connect was working perfectly

But now, I'm getting an error: Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host '
I can get to the mysql database on the server and Godaddy ran scripts that work. They said my parameters were correct but they say it's on my end. I'm talking directly to their server not a localhost. I tried it on my smart phone with a different IP and got the same message. If it's not them and not my code, what can it be? The error is on the $conn = line.
$hostname = "AAAA.server.net";
$username = "BBBB";
$dbname = "CCCCC";
$password = "XXXXX";
$usertable = "tblOne";
//Connecting to database
$conn = mysqli_connect($hostname, $username, $password) OR DIE
("Unable to connect to database! Please try again later.");
mysqli_select_db($conn, $dbname);
Be sure and confirm your database host, because the error message is talking of host problem
Unknown MySQL server host
With GoDaddy, your domain name may not be where your MySQL server resides. It depends upon your server type and your hosting plan. GoDaddy has a help page that will point you in the right direction:
https://support.godaddy.com/help/article/39/viewing-your-database-details-with-shared-hosting-accounts

Categories