Lak
Lak

Reputation: 401

Warning: mysqli_connect(): MySQL server has gone away

I wrote a simple PHP code to connect to the MySQL server as below

<?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 the following errors. I found some topics regarding this issue in google and Stack Overflow. but those don't help me.

( ! ) 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

Upvotes: 5

Views: 37553

Answers (2)

Prasanna
Prasanna

Reputation: 99

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

Upvotes: 1

user4035
user4035

Reputation: 23749

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

$hostname = "localhost";

Upvotes: 7

Related Questions