MySQL, CONCAT, result is null after a while

I am putting big data into a table in a field LONGBLOB

, but as the table grows, the field becomes empty. Code:

mysql_query ('CREATE TABLE IF NOT EXISTS testtable (content LONGBLOB NOT NULL) ENGINE = MyISAM');
mysql_query('TRUNCATE TABLE testtable');
mysql_query('REPLACE INTO testtable VALUES (".")');
$bigData = str_repeat('A', 1024*1024*2); // 2 MB!
foreach (str_split($bigData, 1024*64) as $item)
{
    mysql_query ('UPDATE testtable SET content = CONCAT(content, "'.mysql_real_escape_string($item).'")');
    $rec = mysql_fetch_row(mysql_query ('SELECT content FROM testtable'));
    echo 'Size of the content: '.strlen($rec[0]).'<br>';
}

      

Output:

Size of the content: 65537
Size of the content: 131073
Size of the content: 196609
Size of the content: 262145
Size of the content: 327681
Size of the content: 393217
Size of the content: 458753
Size of the content: 524289
Size of the content: 589825
Size of the content: 655361
Size of the content: 720897
Size of the content: 786433
Size of the content: 851969
Size of the content: 917505
Size of the content: 983041
Size of the content: 0
Size of the content: 65536
Size of the content: 131072
Size of the content: 196608

      

What's happening? LONGBLOB should take more data than this.

+3


source to share


1 answer


Increase the size max_allowed_packet

.

It looks like it fails by 1MB and according to https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html the default max packet size is 1MB:

The default max_allowed_packet is 1MB. This can be increased if the server needs to handle large requests

Set the value in the file my.cnf

, for example:



[mysqld]
max_allowed_packet=16M

      

In PHP

If you don't have access to the MySQL configuration, you can try installing using a query (note: I haven't tested if this will work).

$db->query( 'SET @@global.max_allowed_packet = 16777216' );

      

+2


source







All Articles