I'm getting below warning while connecting to remote database through mysqli_connect.. It is working while connecting with SQLYog
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because
the connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond.
in C:\xampp\htdocs\connect\connect.php on line 9
Code ::
$hostname="xxx.xx.xx.xx";
$username="xxxx";
$password="xxxx";
$database="xxxx";
$port = 3306;
$link = mysqli_connect($hostname, $username, $password, $database, $port);
if (!$link) {
die('Connection failed');
}
Related
I am learning to connect to sql databases using php:
https://www.youtube.com/playlist?list=PLF8OvnCBlEY1bFcTW9JKKO7WJn_Bf9LZ1
But the connection is not made, and the error code is:
Warning: mysqli_connect(): (HY000/2002): Operation timed out in /Users/Ahmead/Sites/PHPTest/myTutorials/dbconnect.php on line 12
cannot connect to database fileld:Operation timed out
myCode php:
<?php
$host="172.0.0.1";
$user="root";
$password="";
$database="admins";
$connect= mysqli_connect($host, $user, $password, $database);
if(mysqli_connect_errno()){
die("cannot connect to database fileld:". mysqli_connect_error());
}
else {
echo 'Database is connected';
}
?>
my code is as below
$dbhost = 'example.com';
$dbuser = 'dbusername';
$dbpass = 'dbpassword';
$dbName='dbname';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbName);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
after i execute code getting below error
Connection failed: Connection refused
Warning: mysqli::__construct(): (HY000/2002): Connection timed out in /home/public_html/test/remote.php on line 8
Connection failed: Connection timed out
I don't know your environment, but my experience is that most publicly accessible webservers do not expose the SQL-database to the public, so the port is probably blocked. If you control the server, then you can probably find something in the firewall, but if you are using something like shared hosting, then you can really only try the helpdesk.
I have created a local php server using XAMPP, for my app to consume.
My app makes a request every second to a php file to check if there is something to do. I've noticed that if I run multiple instantnces of my app, sometimes it starts returning this error:
Warning: mysqli::__construct(): (HY000/2002): Only one
usage of each socket address (protocol/network address/port) is
normally permitted. in C:\xampp\htdocs\testProject\common.php
on line 2 Warning: mysqli_query():
Couldn't fetch mysqli in
C:\xampp\htdocs\testProject\signal_update.php on line
2 Warning: mysqli_error(): Couldn't fetch
mysqli in C:\xampp\htdocs\testProject
The code to connect to the BD is the following:
function openCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "test_database";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
How can I fix this?
Connection failed: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
as i establish connection between my computer and remote machine using "php" and sql server 2008 respectively. i got this error.
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "complaint";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
You must have a defined servername, username and password. If the server is local than use "localhost" if it is a remote server use IP "X.X.X.X"
I cant figure out why i am not connecting to my db. It seems like it is finding the host but having authentication problems or something. I am using xampp and the db provided with it. My connect code is as follows
<?php
$servername = 'localhost:1234';
$username = 'protech';
$password = '';
$dbname = 'protech';
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Connected successfully";
}
mysqli_close($conn);
exit;
?>
if you need any information I am by my computer and will respond quick.
I get these error messages after waiting a while.
Warning: mysqli::__construct(): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): Error while reading greeting packet. PID=7764 in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\protech_connect.php on line 9
The standard port for mySQL is 3306 - so if you are genuinely using 1234 then append that as a new parameter to the db connection constructor.
$dbhost = 'localhost';
$dbuser = 'protech';
$dbpwd = 'xxx';
$dbname = 'protech';
$dbport = 1234;
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname, $dbport );
if( $conn->connect_error ) exit( 'Bad foo: '.$conn->connect_error );
else { /* do exciting things */ }
$conn->close();
The mysqli_connect() function doesn't accept a port in the host parameter. You need to add the port number in another parameter like so:
$conn = mysqli_connect($servername, $username, $password, $dbname, $serverport);
Function documentation: mysqli::__construct()
If you get following error or problem then follow above solution.
Error:
Warning: mysqli_connect(): MySQL server has gone away
Warning: mysqli_connect(): Error while reading greeting packet. PID=3528
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away
Solution 1:
use 127.0.0.1 instead of localhost. Don't use any port number after 127.0.0.1.
Solution 2:
Check your host file located at
C:\Windows\System32\drivers\etc
and remove any localhost or 127.0.0.1 entry in file and save. You can use any HostEditor program to edit this file.