我写了一个简单的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>";
?>
但这会产生以下错误。我在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
答案 0 :(得分:6)
错误在于:
$hostname = "localhost:81";
您没有连接到MySQL,而是连接到Apache服务器。如果您没有更改MySQL端口,请使用
$hostname = "localhost";
答案 1 :(得分:2)
在再次输入数据库名称后忘记指定数据库名称。语法应该是这样的
<?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();
}
?>