mysqli_connect():(HY000 / 2006):MySQL服务器已消失

时间:2019-01-23 01:38:51

标签: php mysql

我收到此错误:

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

当尝试在localhost上运行php文件时。 php7.2 MySQL 8 我已经尝试了许多建议的解决方案几个小时,但没有成功。 这是PHP代码,我只是将其用于其他目的,而不是全部理解它,只是因为我想将数据库连接到应用程序

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

3 个答案:

答案 0 :(得分:1)

Mysql Server使用3306端口作为默认端口。您尝试使用Mysql 81端口连接,而Mysql拒绝使用该端口连接。所以改变

eb local run

$con = mysqli_connect("localhost:81","root","root","testdb") or die ("could not connect to mysql");;
  

顺便说一下,您不需要写端口。默认值为3306。

$con = mysqli_connect("localhost:3306","root","root","testdb") or die ("could not connect to mysql");;

答案 1 :(得分:0)

好吧,出于某种原因,我使用127.0.0.1而不是localhost并一直感谢您的帮助。

答案 2 :(得分:0)

如果你使用的是 xampp 然后检查 mysql 服务器的端口号并更改 这---->>

$con = mysqli_connect("localhost:81","root","root","testdb")

到---->>

$con = mysqli_connect("127.0.0.1:3306","root","root","testdb")

3306的地方你可以输入你的mysql服务器端口号。