php 警告:mysqli_connect():MySQL 服务器已经消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27751556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:30:12  来源:igfitidea点击:

Warning: mysqli_connect(): MySQL server has gone away

phpmysqlmysqlidatabase-connection

提问by Lak

I wrote a simple php code to connect to the mysql server as below

我写了一个简单的php代码来连接到mysql服务器,如下所示

    <?php

$username = "root";
$password = "Kepwd";
$hostname = "localhost:81";

//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

 ?>

but this generates following errors. I found some topics regarding this issue in google and stactoverflow. but those aren't help me. please can anyone help me ?

但这会产生以下错误。我在 google 和 stactoverflow 中找到了一些关于这个问题的主题。但那些对我没有帮助。请任何人都可以帮助我吗?

    ( ! ) Warning: mysqli_connect(): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8

( ! ) Warning: mysqli_connect(): Error while reading greeting packet. PID=10612 in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8

( ! ) Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8
Unable to connect to MySQL

回答by user4035

The error is here:

错误在这里:

$hostname = "localhost:81";

You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use

您没有连接到 MySQL,而是连接到 Apache 服务器。如果您没有更改 MySQL 端口,请使用

$hostname = "localhost";

回答by Prasanna

you forget to specify the database name after entering database name try again. The syntax should be like this

输入数据库名称后忘记指定数据库名称再试一次。语法应该是这样的

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>