How to get mysqli_connect to work? - php

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.

Related

Connection refused "mysqli_connect(): (HY000/2002): Connection refused in **************************conn.php on line 6 Failed

Hey guys I have problem with connecting my .php file to database, I've tried different ways but still the same response.
Here is my code (updated):
<?php
$server_name = "localhost";
$mysql_username = "*****";
$mysql_password = "*****";
$db_name = "*****";
$conn = mysqli_connect ($server_name, $mysql_username, $mysql_password, $db_name);
if($conn) {
echo "Success";
}
else {
echo "Failed";
}
?>
UPDATE
Guys, when I connect to localhost everything works fine but I would like to connect to a server and be able to upload data from different android devices. Currently I use 000webhost.com free service, so might it be the key point and problem that I didn't upgrade that to PRO?
mysqli_connect requires the arguments to be in the order servername, username, password, databasename. Your order is servername, databasename, username, password.
The code should be:
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
You sequence of parameter is wrong
<?php
$server_name = "*****";
$db_name = "*****";
$mysql_username = "*****";
$mysql_password = "*****";
$conn = mysqli_connect($server_name, $mysql_username,$mysql_password, $db_name);
if($conn) {
echo "Success";
} else {
echo "Failed";
} ?>
Correct syntax
mysqli_connect(host,username,password,dbname,port,socket);
Reference mysqi_connect
EDIT
A extra tip
$conn = mysqli_connect("localhost","username","password","db_name");
Put values according to you server.
EDIT 2
MySQL needs to be running on the port 3306 make sure it is correct
From the comments and from the ruling out of other possibilities indicated in the other answers, it is quite possible it is a connection issue with your service provider.
You should write to them explaining your situation, if at all they have support for free services. You should probably change the passwords to some dummy ones in case you have share it with them; Some service providers ask you to share your password for them to check, not sure if it's recommended practice.
Also, for testing and development, it's better you use some kind of local installation of PHP, such as XAMPP or WAMPP etc, so that you can have control during development and testing. If something works on your local installation and not on the remote, having a development installation is useful to isolate and report such issues.
I think your problem is that you did not instantiate the class, remedied by making it $conn = new mysqli ($server_name, ..... . Also, your sequence of parameters is incorrect. It should be server, user, password and finally database.
In addition, you should be using PHP constants instead of variables for this, so $server_name should be SERVER_NAME. Similarly $mysqli_username, $mysqli_password, and $db_name should be changed. Not that I wouldn't work as you have it, but it just is not proper.
Iy you are using MAMP, the default port for mysql is 8889, but the "traditional" port that php expects to use for mysql is 3306.
So you need to open MAMP and change the MAMP mysql port to 3306, then restart the mysql server. Now the connection should be successful.

Connecting to a MySQL db created in an Ubuntu AWS instance

Ok so I created an ubuntu instance from AWS and downloaded LAMP on it as you would in a regular Ubuntu OS. When I try to connect to the db from my php script it's rejecting the connection. saying "Connection failed: Connection refused". So I guess my question is this: Does amazon not allow you to connect to a db without using their RDS database service or am I putting something wrong here? (I've hidden some of the data for security purposes, the ... are numbers of my instance). When I put "ec2-34-...-..-64.compute-1.amazonaws.com" into the browser the apache message comes up so I don't see why this is not working as a server name?
$servername = "ec2-34-...-..-64.compute-1.amazonaws.com";
$username = "root";
$password = “hidden";
$dbname = "questions87";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user#YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.
Reference and further reading:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

mysqli_connect connection error in GoDaddy

My system using core php and it is working fine in localhost. When I host into live then there is an error occurs site not working. I hosted in GoDaddy server and below are the code.
//connection code
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "******";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
But i get below error in browser
PHP Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it.
in G:\PleskVhosts\projectname.com\httpdocs\pages\header.php on line 13
Please help me to fix it.
Not a problem in your code. May be service is not listing on your port or firewall is blocking your connection. In this case most possible reason is firewall.
You need to try "netstat -anb" for checking a running expected port. Here is a link my be help you:
No connection could be made because the target machine actively refused it?

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

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

Categories