MySQL,CONCAT,过一会结果为null

标签 mysql

我将大数据放入表中,放入 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_pa​​cket 大小。

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

The server's default max_allowed_packet value is 1MB. You can increase this if the server needs to handle big queries

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

[mysqld]
max_allowed_packet=16M

在 PHP 中

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

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

关于MySQL,CONCAT,过一会结果为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30936310/

相关文章:

mysql - 从 PHP 应用程序导入 Rails 应用程序时出现 UTF-8 编码问题

MySQL正则表达式选择字符串中的最后一个整数

php - 数据库中的查询获取

MySQL 权限——即使使用 'CREATE ROUTINE' 授权也无法创建函数

php - 从MySQL数据库获取数据时查询失败

mysql - 使用别名优化 Select 子查询

Mysql查找重复项

mysql - 如何在两个不同数据库之间的mysql中设置外键?

mysql - 如何返回不同列中的 SQL max() 值?

mysql - 使用java struts插入由mysql表中的字段创建的