Ahmed Sedik
Ahmed Sedik

Reputation: 81

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

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);
?>

Upvotes: 0

Views: 6202

Answers (3)

Deepak Silver
Deepak Silver

Reputation: 27

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.

Upvotes: 0

Ahmed Sedik
Ahmed Sedik

Reputation: 81

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

Upvotes: 1

Omer Tekbiyik
Omer Tekbiyik

Reputation: 4764

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");;

Upvotes: 2

Related Questions