我收到此错误:
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);
?>
最佳答案
Mysql Server使用3306端口作为默认端口。您正在尝试与Mysql 81端口连接,而Mysql拒绝与该端口连接。所以改变
$con = mysqli_connect("localhost:81","root","root","testdb") or die ("could not connect to mysql");;
至
$con = mysqli_connect("localhost:3306","root","root","testdb") or die ("could not connect to mysql");;
顺便说一下,您不需要写端口。默认值为3306。
$con = mysqli_connect("localhost","root","root","testdb") or die ("could not connect to mysql");;