[Solved] the logic of calculating packet size.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

[Solved] the logic of calculating packet size.

Post 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:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

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

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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.
Post Reply