[英]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.尝试在本地主机上运行 php 文件时。 php7.2 MySQL 8 i have tried alot of suggested soltutions on for hours but no success. php7.2 MySQL 8 我已经尝试了很多小时的建议解决方案,但没有成功。 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 代码,并不了解所有内容,但只是因为我想将数据库连接到可能的应用程序
<?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. Mysql Server 默认使用 3306 端口。 You are trying to connect with Mysql 81 port and Mysql refused to connect with that port.您正在尝试连接 Mysql 81 端口,而 Mysql 拒绝连接该端口。 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. 3306 是默认值。
$con = mysqli_connect("localhost","root","root","testdb") or die ("could not connect to mysql");;
好吧,出于某种原因,我使用了127.0.0.1
而不是localhost
并且工作了无论如何感谢您的帮助。
if you are using xampp then check the port number of mysql server and change this ---->>如果您使用的是xampp,请检查 mysql 服务器的端口号并更改此 ---->>
$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.在3306的位置,您可以输入您的 mysql 服务器端口号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.