Warning: mysqli_connect (): MySQL server is gone

I wrote simple PHP code to connect to mysql server as shown 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 several threads dealing with this issue on google and stactoverflow. but that doesn't help me. please can anyone 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

      

+3


source to share


2 answers


The error is here:

$hostname = "localhost:81";

      



You are not connecting to MySQL, but to Apache server. If you haven't changed the MySQL port, just use

$hostname = "localhost";

      

+6


source


you forgot to specify the database name after entering the database name again. The syntax should look 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();
}
?>

      

+2


source







All Articles