This question already has answers here:
PHP Connection failed: SQLSTATE[HY000] [2002] Connection refused
(10 answers)
Closed 3 years ago.
I am new in php and doing a login/register page in localhost using xampp on mac. I am having a trouble with this error:
Warning: mysqli_connect(): (HY000/2002): Connection refused in /opt/lampp/htdocs/website/includes/dbh.inc.php on line 9
Connection failed: Connection refused
I have tried changing the code but it continuously adding more problems.
This is my code:
<?php
$servername = "192.168.64.2:8080";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
How can I eliminate this error?
If you want to connect to other port than the default one, add the port argument when you establish the connection, not define it within the server name.
$servername = "192.168.64.2";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";
$dbPort = "8080";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName, $dbPort);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
Another thing is to make sure you have the right port of MySQL service, port 8080 is commonly picked as the alternative for port 80 to handle HTTP request. Check your XAMPP configuration to find out which port is used for MySQL.
Related
This question already has answers here:
How to use PHP to connect to sql server
(17 answers)
Using PHP connect Microsoft SQL Server 2014
(1 answer)
PHP script to connect to SQL Server
(1 answer)
Connect to SQL Server using PHP?
(1 answer)
Closed 27 days ago.
I'm bulding an Intranet for my workplace and I need to connect it to a Database. I already have a script that connect my website throught XAMPP on PHPmyadmin, but now my boss asked me to connect the website to a database we have on Microsoft SQL server Management program and maybe I'm stupid but I'm not understanding how to do it and what's the difference.
And also: to connect to Microsoft SQL i use windows authentication so I dont know what to write on the php script.
This is my connect.php script I have and works on PHPMYADMIN:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "intranet";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
And this is the one I wrote for the database I have on Microsoft SQL (I change the server cause it's the one I use to connect when I open the program):
<?php
$servername = "EDP004\SQLEXPRESS";
$username = "root";
$password = "";
$dbname = "INTRANET_DD";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
And this is the error I get on browser:
Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo for EDP004\SQLEXPRESS failed: unknown Host. in C:\xampp\htdocs\log\material\center\html\scripts\config.php on line 8
Fatal error: Uncaught mysqli_sql_exception: php_network_getaddresses: getaddrinfo for EDP004\SQLEXPRESS failed: unknown Host. in C:\xampp\htdocs\log\material\center\html\scripts\config.php:8 Stack trace: #0 C:\xampp\htdocs\log\material\center\html\scripts\config.php(8): mysqli->__construct('EDP004\\SQLEXPRE...', '', '', 'INTRANET_DD') #1 C:\xampp\htdocs\log\material\center\html\index.php(2): require_once('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\log\material\center\html\scripts\config.php on line 8
I have bought a database MySQL to use on my domain/host.
I can connect to the database via wampserver (localhost) without problems.
But when I am trying to connect to the host website it gives me an error.
Connection failed: Can't connect to MySQL server on 'mysql-******.net' (111 "Connection refused")
My code:
<?php
$servername = "mysql-*********.net";
$username = "username8954";
$password = "dolphyn1235";
$dbname = "animals";
$dbServerPort = "26313";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $dbServerPort);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Any ideas how to solve this?
Check in your MySQL Server configuration files (my.cnf), find bind-address = 127.0.0.1 then comment them with # at the begining of the line. and then restar your MySQL.
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.
This question already has answers here:
Message: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known
(2 answers)
Closed 1 year ago.
I tried to connect to phpMyAdmin database but encounter an error Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known. Below coding is the one that I currently use.
$servername = "http://localhost:8080";
$username = "root";
$password = "";
$db = "dbLogin";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
else {
echo "Connection success!";
}
When I run my system, It will still show the message connection success! but together with the error I mentioned.
replace server name from
$servername = "http://localhost:8080";
to
$servername = "localhost";
I have one project running a database on a Cloud9 project. I have created a mobile app which will need to connect to the database by POST to a PHP file which will then run the following code:
$servername = "myusername-projectname-somenumber";
$port = 3306;
$username = "form";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, 3306);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I got the servername by using this in the terminal:
SHOW VARIABLES WHERE Variable_name = 'hostname';
I am getting an error from the other project:
Connection failed: Unknown MySQL server host 'myusername-projectname-somenumber' (0)
***Note: I have replaced myusername-projectname-somenumber and dbname for posting this. I did not forget to fill them out.
Thanks for the help!