i was trying to insert some data into the database but i got this error \"Error while sending QUERY packet\"
$insertDeta = $conPat->pre
You can solve this problem by following few steps:
1) open your terminal window
2) please write following command in your terminal
ssh root@yourIP port
3) Enter root password
4) Now edit your server my.cnf file using below command
nano /etc/my.cnf
if command is not recognized do this first or try vi then repeat: yum install nano.
OR
vi /etc/my.cnf
5) Add the line under the [MYSQLD] section. :
max_allowed_packet=524288000 (obviously adjust size for whatever you need)
wait_timeout = 100
6) Control + O (save) then ENTER (confirm) then Control + X (exit file)
7) Then restart your mysql server by following command
/etc/init.d/mysql stop
/etc/init.d/mysql start
8) You can verify by going into PHPMyAdmin or opening a SQL command window and executing:
SHOW VARIABLES LIKE 'max_allowed_packet'
This works for me. I hope it should work for you.
You guessed right MySQL have limitation for size of data, you need to break your query in small group of records or you can Change your max_allowed_packet by using SET GLOBAL max_allowed_packet=524288000;
In /etc/my.cnf
add:
max_allowed_packet=32M
It worked for me. You can verify by going into PHPMyAdmin and opening a SQL command window and executing:
SHOW VARIABLES LIKE 'max_allowed_packet'
If inserting 'too much data' fails due to the max_allowed_packet
setting of the database server, the following warning is raised:
SQLSTATE[08S01]: Communication link failure: 1153 Got a packet bigger than
'max_allowed_packet' bytes
If this warning is catched as exception (due to the set error handler), the database connection is (probably) lost but the application doesn't know about this (failing inserts can have several causes). The next query in line, which can be as simple as:
SELECT 1 FROM DUAL
Will then fail with the error this SO-question started:
Error while sending QUERY packet. PID=18486
Simple test script to reproduce my explanation, try it with and without the error handler to see the difference in impact:
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
try
{
// $oDb is instance of PDO
var_dump($oDb->query('SELECT 1 FROM DUAL'));
$oStatement = $oDb->prepare('INSERT INTO `test` (`id`, `message`) VALUES (NULL, :message);');
$oStatement->bindParam(':message', $largetext, PDO::PARAM_STR);
var_dump($oStatement->execute());
}
catch(Exception $e)
{
$e->getMessage();
}
var_dump($oDb->query('SELECT 2 FROM DUAL'));
You cannot have the WHERE
clause in an INSERT
statement.
insert into table1(data) VALUES(:data) where sno ='45830'
Should be
insert into table1(data) VALUES(:data)
Update: You have removed that from your code (I assume you copied the code in wrong). You want to increase your allowed packet size:
SET GLOBAL max_allowed_packet=32M
Change the 32M
(32 megabytes) up/down as required. Here is a link to the MySQL documentation on the subject.
I encountered a rare edge case in cygwin, where I would get this error when doing exec('rsync');
somewhere before the query. Might be a general PHP problem, but I could only reproduce this in cygwin with rsync.
$pdo = new PDO('mysql:host=127.0.0.1;dbname=mysql', 'root');
var_dump($pdo->query('SELECT * FROM db'));
exec('rsync');
var_dump($pdo->query('SELECT * FROM db'));
produces
object(PDOStatement)#2 (1) {
["queryString"]=>
string(16) "SELECT * FROM db"
}
PHP Warning: Error while sending QUERY packet. PID=15036 in test.php on line 5
bool(false)
Bug reported in https://cygwin.com/ml/cygwin/2017-05/msg00272.html