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.
Related
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?
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.
I have a back end cron script that checks MySQl regurlarly for an if-then condition and then runs a Curl action. Anyway, the script has been running fine for over a year now when suddenly a problem occured and it does not make sense to me
here is the truncated code for the script
<?php
//assign values to variables
$servername = "localhost:3306";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect($servername, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = "SELECT NewUserID FROM queue ORDER BY ID";
$queryresults = $con->query($results);
if($con->error) {
printf("Errormessage: %s\n", $con->error);
die();
}
I would see this error:
Warning: mysqli_connect(): (HY000/2002): Failed to parse address "localhost:3306:3306" in /home/foo/bar/BackEnd.php on line 11
Failed to connect to MySQL: Failed to parse address "localhost:3306:3306"
Fatal error: Uncaught Error: Call to a member function query() on boolean in /home/foo/bar/BackEnd.php:19
Stack trace:
#0 {main}
thrown in /home/foo/bar/BackEnd.php on line 19
The solution was simple, I change "localhost:3306" to "localhost" and everything worked again, since it seemed that ":3306" was being added to what was already there. This solution seems like a problem waiting to happen but im not sure what went wrong in the first place. Any ideas?
You can't specify the port in the host parameter.
Here is the full function definition:
mysqli_connect($host, $username, $password, $dbname, $port, $socket);
You should just add the port at the end of your call. Like this:
$con = mysqli_connect($servername, $username, $password, $database, 3306);
I'm trying to learn php but when I attempt to connect to a mysql database, I get this error. I don't think anything is wrong with the code itself but perhaps there is an error on the hosts side? I'm using 000webhost so.
Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied
for user 'a7976620_db1user'#'localhost' (using password: YES) in
/home/a7976620/public_html/index.php on line 17
Here is my php:
<?php
$servername = "localhost";
$username = "a7976620_db1user";
$password = "******";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
In https://www.000webhost.com, there are two options in the cPanel:
Fix ownership and
Fix file permissions
You can apply this and it will be fine.