Viewed   183 times

I'm trying to simply connect to a MySQL database on my localhost With php and mysqli. But I'm encountering a problem.

I Wrote the code below

$sql = new mysqli('','root','Qwert12345','plot_io_db');
//echo $sql->query('Select * From players');

Assuming that all the information is correct (there is a MySQL server running and it includes a database with the name 'plot_io_db' and username and the password are correct), it still takes ages for the script to return results and it returns the following result

Warning: mysqli::__construct(): MySQL server has gone away in C:xampphtdocstestdefault.php on line 2
Warning: mysqli::__construct(): Error while reading greeting packet. PID=11092 in C:xampphtdocstestdefault.php on line 2
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in C:xampphtdocstestdefault.php on line 2
Fatal error: Maximum execution time of 30 seconds exceeded in C:xampphtdocstestdefault.php on line 2

My question is: Why does this happen and how to solve this.

P.S: I commented the third line just to bring the result time below ten minutes!


$sql = new mysqli('','root','Qwert12345','plot_io_db');
//echo $sql->query('Select * From players');

It will work. Just remove port from localhost (

Tuesday, August 30, 2022

From your edit, the issue appears to be a scoping one.

You're attempting to make a connection inside a function where the $username, $password and $database variables are defined outside that function.

I suggest you read the Variable Scope section of the manual and turn up your error reporting as @netcoder suggests in the question comments.

Sunday, December 4, 2022

Your connection has been closed, after execution of first SELECT Statement, it means connection closed to early:


You need to use close() after your all queries or re build connection. ist one is the better option.

You are getting user input $_REQUEST['key'], it means your query is open for SQL injection, this will help you to understand how can you prevent your code with SQL injection: How can I prevent SQL injection in PHP?

Wednesday, August 3, 2022

mysqli_connect("","username" ,"password","databasename");//Server name cannot be NULL

use loaclhost for server name(In Loacl)

    $con = mysqli_connect("localhost","username" ,"password","databasename");

    if (mysqli_connect_errno())
        echo "Failed to connect to MySQL: " . mysqli_connect_error();

Or can use MySQLi Procedural

    $servername = "localhost";
    $username = "username";
    $password = "password";

    // Create connection
    $con = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$con) {
        die("Connection failed: " . mysqli_connect_error());
    echo "Connected successfully";


$servername = "localhost";
$username = "root";
$password = "";
Monday, October 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :