What is wrong with my connection? PHP mysql

Hello, my page time to time trows out this error:

Warning: mysqli_query(): MySQL server has gone away in /storage/files/domain.com/see.php on line 6

Warning: mysqli_query(): Error reading result set’s header in /storage/files/domain.com/see.php on line 6

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /storage/files/domain.com/see.php on line 8

here is see.php

include('connection.php');
session_start();
$user_check=$_SESSION['username'];

$ses_sql = mysqli_query($db,"SELECT user FROM my_users WHERE user='$user_check' ");

$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_user=$row['user'];

if(!isset($user_check))
{
header("Location: index.php");
exit;
}

And here is connection.php

define('DB_SERVER', '127.0.0.1');
define('DB_USERNAME', 'myusername');
define('DB_PASSWORD', 'mypassword');
define('DB_DATABASE', 'mydatabase');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

$url = "http://www.mysite.com/";
//application address
define('DIR','http://mysite.com/');
define('SITEEMAIL','admin@mysite.com');


include('classes/phpmailer/mail.php');

I m not that good in finding such an problems and i just cant see nothing bad here :frowning:
Whats wrong?

As I understand it session_start(); should come at the start of the page.

I would start from basics and try your code without the session or other code to see if it is connecting and getting a result.

you mean it need to before include?

starting with basics is a good idea always!

We always think we know basics . … I m here for help!

Quite some time ago, I moved hosting to Godaddy and for about a month had similar Mysql problems which were eventually resolved after this support ticket:

As I understand it, the key thing is that your session_start() is called before any output to the browser, because the function attempts to set a cookie, which can’t be done once browser output has started. So in your case, as long as the connection.php include file doesn’t output to the browser, then it doesn’t matter that it appears before the session is started.

1 Like

Well my hosting says its scripts foult, that probably looping over and over :frowning: but i cant find such issue!

So that is not the issue correct?

It would be a good idea to check whether the connect to database was successful or not before you do anything else.

2 Likes

Yes, there’s a lot of checking missing. There’s no check that $_SESSION['username'] exists before assigning it to $user_check` and using it in a query, and there’s also no check whether the query returned anything before fetching the details and using them for another variable.

I presumed that error message really indicated some sort of timeout, but the documentation also suggests it can occur if the database connection has been closed.

I doubt it - sending session_start() at the wrong time would normally just give a “headers already sent” error message.

2 Likes

this is not code issue you have to Check the value of “max_allowed_packet” in your my.cnf MySQL config file.

Detail Answer : https://stackoverflow.com/questions/11583352/php-mysql-server-has-gone-away

1 Like

I will contact my hosting about this!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.