Page 1 of 1

[Solved] the logic of calculating packet size.

Posted: Thu Jan 29, 2009 12:50 pm
by kaisellgren
Hello there,

I've been trying to calculate my packet size without success. I'm using MySQL at the moment and it seems to give me wierd results.

Code: Select all

...nevermind.... better example below
...look below...

EDIT: Here's a better demonstration:

Code: Select all

$a = mysql_connect('localhost','root','****'); 
mysql_select_db('test',$a);
 
//mysql_query('SET @@global.max_allowed_packet=1024;');
 
$r = mysql_query('SELECT @@global.max_allowed_packet AS mpsize;',$a);
$f = mysql_fetch_assoc($r);
echo $f['mpsize'].'<br>';
 
$data = str_repeat('A',1024*15); // making some data
$query = "INSERT INTO `dbtest` (`test2`) VALUES ('$data');";
 
echo strlen($query).'<br>';
mysql_query($query) or die(mysql_error());
The following outputs:
1024
15403
So I managed to put 15403 bytes into a packet that has a max size of 1024 bytes :crazy:

EDIT2: With 1024*16, it does not work:
1024
16427
MySQL server has gone away
EDIT3: Solved! :D I was looking into wrong property. Oh dear. :banghead:

Re: [Solved] the logic of calculating packet size.

Posted: Thu Jan 29, 2009 8:28 pm
by josh
I might be wrong but packet size != strlen. strlen will tell you the # of characters, furthermore if you're using a single byte charset it will tell u bytesize of the query itself, but packets have headers, which count towards their size, and a long query would be split among multiple packets, I'm not sure PHP out of the box offers access to that low of a network layer, you can read and write over sockets but I'm pretty sure all the packet handling stuff is encapsulated by some OS / library code or C routine.

Re: [Solved] the logic of calculating packet size.

Posted: Fri Jan 30, 2009 8:00 am
by kaisellgren
jshpro2 wrote:I might be wrong but packet size != strlen. strlen will tell you the # of characters, furthermore if you're using a single byte charset it will tell u bytesize of the query itself, but packets have headers, which count towards their size, and a long query would be split among multiple packets, I'm not sure PHP out of the box offers access to that low of a network layer, you can read and write over sockets but I'm pretty sure all the packet handling stuff is encapsulated by some OS / library code or C routine.
No the problem was that max_packet_size does not do exactly what you think. net_buffer_length did what I needed. If strlen() of query is more than net_buffer_length then it's an error.