我将大数据放入表中的LONGBLOB字段,但是随着表的增长,该字段变为空。代码:

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>';
}

输出:
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

发生什么事了?longblob应该会获取更多的数据。

最佳答案

增大max_allowed_packet大小。
它看起来在1MB时失败,根据https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html的默认最大数据包大小是1MB:
服务器默认允许的最大数据包值为1MB。如果服务器需要处理大型查询,可以增加此值
my.cnf文件中设置值,例如:

[mysqld]
max_allowed_packet=16M

在PHP中
如果您没有mysql配置的访问权限,可以通过查询尝试设置(注意:我没有检查这是否有效)。
$db->query( 'SET @@global.max_allowed_packet = 16777216' );

10-08 13:29