首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL,CONCAT,结果在一段时间后为空。

MySQL,CONCAT,结果在一段时间后为空。
EN

Stack Overflow用户
提问于 2015-06-19 10:51:04
回答 1查看 89关注 0票数 3

我将大数据放到表中,放到一个LONGBLOB字段中,但是随着表的增长,字段就变成了空的。代码:

代码语言:javascript
复制
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>';
}

产出:

代码语言:javascript
复制
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需要更多的数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-19 10:59:19

增加max_allowed_packet大小。

看起来它在1MB时失败了,根据https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html,默认的最大数据包大小是1MB:

服务器的默认max_allowed_packet值为1MB。如果服务器需要处理大型查询,则可以增加此值。

my.cnf文件中设置值,例如:

代码语言:javascript
复制
[mysqld]
max_allowed_packet=16M

在PHP中

如果您没有访问MySQL配置的权限,可以尝试通过查询进行设置(注意:我没有检查这是否有效)。

代码语言:javascript
复制
$db->query( 'SET @@global.max_allowed_packet = 16777216' );
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30936310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档