mysqli_connect(): (HY000/2006): MySQL server has gone away - php

I get this error:
Warning: mysqli_connect(): MySQL server has gone away in C:\Apache24\htdocs\soccer.php on line 5
Warning: mysqli_connect(): Error while reading greeting packet. PID=19176 in C:\Apache24\htdocs\soccer.php on line 5
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\Apache24\htdocs\soccer.php on line 5
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Apache24\htdocs\soccer.php on line 5
when trying to run a php file on localhost.
php7.2 MySQL 8
i have tried alot of suggested soltutions on for hours but no success.
here is the PHP code which i am just using for other purpose and dont understand all of it but just because i want to connect a database to may Application
<?php
$con = mysqli_connect("localhost:81","root","root","testdb") or die ("could not connect to mysql");;
if(mysqli_connect_error($con))
{
echo "Failed to connect";
}
$query = mysqli_query($con,"SELECT * FROM testtb");
if($query){
while($row=mysqli_fetch_assoc($query))
{
$flag[] = $row;
}
print(json_encode($flag));
mysql_free_result($query);
}
mysqli_close($con);
?>

Mysql Server use 3306 port as a default. You are trying to connect with Mysql 81 port and Mysql refused to connect with that port. So change
$con = mysqli_connect("localhost:81","root","root","testdb") or die ("could not connect to mysql");;
to
$con = mysqli_connect("localhost:3306","root","root","testdb") or die ("could not connect to mysql");;
By the way yo don't need to write port. 3306 is default.
$con = mysqli_connect("localhost","root","root","testdb") or die ("could not connect to mysql");;

well for some reason i used 127.0.0.1 instead of localhost and worked Thanks anyway for the help.

if you are using xampp then check the port number of mysql server and change
this ---->>
$con = mysqli_connect("localhost:81","root","root","testdb")
to ---->>
$con = mysqli_connect("127.0.0.1:3306","root","root","testdb")
In the place of 3306 you can put your mysql server port number.

Related

when i used to connect with phpmyadmin using netbeans in php programming language i got some error

This is my code for connecting to database:
$datbase=mysqli_connect("localhost:82","root"," ");
if(!$datbase)
{
die("connection failed".mysqli_connect_error());
}
it shows me that
Warning: mysqli_connect(): MySQL server has gone away in C:\Program Files\apache24\Apache24\htdocs\PhpProject3\php_DataBase.php on line 9
Warning: mysqli_connect(): Error while reading greeting packet. PID=10848 in C:\Program Files\apache24\Apache24\htdocs\PhpProject3\php_DataBase.php on line 9
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\Program Files\apache24\Apache24\htdocs\PhpProject3\php_DataBase.php on line 9
line number 9 is database=mysqli_connect("localhost:82","root"," ")
Error in this line and also remove space in password
$datbase=mysqli_connect("localhost:82","root"," ");
You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use
$datbase=mysqli_connect("localhost","root","","databaseName");
Replace this line with
$datbase=mysqli_connect("localhost:82","root"," ");
with this one
$datbase=mysqli_connect("localhost","root","","your database name");
If your Mysql server is runs with default settings means . you don't need specify the port number . otherwise you need to specify port number as 5th parameter . like below Example .
Mysql default port number is 3306
$datbase=mysqli_connect("localhost","username","password","database_name","port_number");
if(!$datbase)
{
die("connection failed".mysqli_connect_error());
}
You should read first http://php.net/manual/en/function.mysqli-connect.php.
$link = mysqli_connect("host", "my_user", "my_password", "my_db");
Try this:
$datbase=mysqli_connect("localhost","root","","my_db");
if(!$datbase)
{
die("connection failed".mysqli_connect_error());
}
you are using port number on localhost that you are not set in php.ini file
try like this
//$datbase=mysqli_connect("localhost","username","password","databaseName");
$datbase=mysqli_connect("localhost","root","","databaseName");

Warning: mysqli_connect(): Unknown MySQL server host

I have some trouble connecting to a mysql server. I can connect using the linux terminal, so I know that my host, port, user, and password works. I cannot, however, connect using PHP.
PHP Version: 5.2.17
Server version: 5.5.27-log MySQL Community Server (GPL)
Here is a test code example:
<?php
$link = mysqli_connect('host.com:5306', 'user', 'pass', 'db');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!mysqli_query($link, "SET a=1")) {
printf("Errormessage: %s\n", mysqli_error($link));
}
/* close connection */
mysqli_close($link);
?>
Gives the following warning:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'host.com:5306' (1) in /.../test.php on line 3
Connect failed: Unknown MySQL server host 'host.com:5306' (1)
Any ideas on what to do?
The port number must be a separate argument:
$link = mysqli_connect('host', 'user', 'pass', 'db', 5306);
Any ideas on what to do?
Sure. A simple three-step solution for any php function's problem:
Open your favorite browser
type php.net/ in the address bar followed by a problem function's name:
`php.net/mysqli_connect` in your case
Hit Enter
Now you have the function's description and can check the proper parameters list.
host is the IP of your mysql server, or maybe "localhost"
The host parameter in the mysql function need to be "localhost" or "127.0.0.1"
You set the parameter as "host:port" and that is your mistake. It's need to be the full alias of the network in the server or the remote server with the full IP address. You can found more information about this error here: http://compnetworking.about.com/od/workingwithipaddresses/g/127_0_0_1_def.htm
The mysql book in php.net: http://www.php.net/manual/en/function.mysql-connect.php

This MySQLI query will not work/ Is there an issue with my web server SQL settings?

This is the code I have for the query:
$query = "SELECT * FROM articles LIMIT 1";
$mysqli = mysqli_connect($host, $user , $pass , $database);
$res = mysqli_query($mysqli, $query) or
die("Couldn't execute query.");
$row = mysqli_fetch_assoc($res);
echo $row;
Does anyone see any obvious problems with this that would stop it from executing? $host, $user etc are defined in a separate config file.
This is the error message I get:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002):
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in
/websites/123reg/LinuxPackage22/bu/ck/et/public_html/admin/config.php on line 8
Connect failed: Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)
I have read that this means there is an issue with an SQL setting, can anyone confirm this?
Your mysql did not use default location for the socket, which is /var/lib/mysql/mysql.sock.
So, check your configuration to find out the actual socket file, and replace the connection string accordingly, see mysqli_connect

Connection to MySQL server lost (error:111)

I have a php file to fetch data from a server
When I try to execute it, I get a message saying:-
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/-/-/public_html/GetData.php on line 3
And line 3 is
$con = mysql_connect("cs.neiu.edu","myusername","mypassword");
Also I've tried by giving the hostname as localhost,but its not working.
You need to configure binding address bind-address in my.cnf and grand access to the database eg. GRANT ALL ON foo.* TO bar#'162.54.10.20' IDENTIFIED BY 'PASSWORD';
That error means connection refused. I am in the same class that you are and obviously I am using the same server, and I would guess that you are entering the wrong password. It may not be the same password that you connect to the server with if you changed your password from what was given in class. I use the following and it works fine....
<?php
$con = mysql_connect('localhost','username','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
..
..
..
mysql_close($con);
?>
Actually i realized that my password was wrong.

Cant connect to mysql: MySQL server has gone away

I'm start to learn mysql not so long time ago, and now i have problem, that i cant solve by myself.
I'm trying to connect to mysql server with php. My admin gaved me ip, port, login, pass and db name.
Php file looks like this:
$hostname = '192.168.1.20:1433';
$username = "h";
$password = "h";
$dbName = "mydb";
mysql_connect($hostname,$username,$password) OR DIE(mysql_error());
mysql_select_db($dbName) or die(mysql_error());
$query = "SELECT * FROM `insy` WHERE 1;";
$result=mysql_query($query) or die(mysql_error());
echo "works!";
mysql_close();
When i run page, its do nothing few minutes, than errors appears:
Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12
Warning: mysql_connect() [function.mysql-connect]: Error while reading greeting packet. PID=1908 in N:\home\192.168.1.19\www\phlib.php on line 12
Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12
MySQL server has gone away
12'th line is:
mysql_connect($hostname,$username,$password) OR DIE(mysql_error());
I tried to create ODBC Sql Server via Control Panel -> Administration - with same parameters - it works, connection is ok, and i ca see the Db's.
Tell me please, where is my fault?
I hope, i was clear.
Thanks for help.
you can try mysql_ping($conn) if your server connection lost.
http://php.net/manual/en/function.mysql-ping.php
I've just solved that problem.. the thing was when I did create a new user in MySql, after writing the user, the server ask you for where will be the "server" and you put localhost:8080.. ERROR! don't put the port!! just localhost.
And thats how I solved it..

Categories