【问题标题】:how insert large data to MySQL database without change max_allowed_packet如何在不更改 max_allowed_pa​​cket 的情况下将大数据插入 MySQL 数据库
【发布时间】:2012-10-05 14:50:30
【问题描述】:

我的文本大于 max_allowed_packet (1MB) 为什么我不能用下面的代码插入数据?

$db = new PDO('mysql:dbname=test;host=localhost', 'root', '5u4f1d');

$db->exec('SET NAMES utf8');

$stmt = $db->prepare('INSERT `book` (`text`) VALUES (?)');
$tx = file_get_contents('./test.html');
$stmt->bindParam(1, $tx, PDO::PARAM_LOB);

$db->beginTransaction();
$stmt->execute();
$db->commit();

上面写着:Fatal error: Cannot pass parameter 1 by reference,但我从以下位置复制代码: - http://www.php.net/manual/en/pdo.lobs.php

数据库结构:

CREATE TABLE `book`
(`text` mediumtext COLLATE utf8_unicode_ci NOT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

【问题讨论】:

  • file_get_contents 是返回任何值还是为空?
  • @Zlatan 是的,它返回大文本。

标签: php mysql pdo insert large-data


【解决方案1】:

试试这样:

$db = new PDO('mysql:dbname=test;host=localhost', 'root', '5u4f1d');

$db->exec('SET NAMES utf8');

$stmt = $db->prepare('INSERT `book` (`text`) VALUES (?)');

// note the "fopen" function, not file_get_contents
$tx = fopen('test.html', 'rb');
$stmt->bindParam(1, $tx, PDO::PARAM_LOB);

$db->beginTransaction();
$stmt->execute();
$db->commit();

还有:

指出:

... 将 LOB 绑定到名为 $lob 的变量中,然后将其发送到 浏览器使用 fpassthru()。由于 LOB 表示为 流,函数如 fgets()、fread() 和 stream_get_contents() 可以用在上面。

另外,尝试将“文本”表字段更改为 BLOB 数据类型,如下所示:

CREATE TABLE `book`
(`text` BLOB NOT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

【讨论】:

  • 不,它不能解决问题。您对fpassthru 的引用是关于 SELECT(读取)而不是 INSERT
  • 是的,我尝试使用 $tx = fopen('test.html', 'rb'); 而不是 $tx = file_get_contents('./test.html'); 来解决问题,但问题没有解决。
  • 是“BLOB”数据类型表中的“文本”字段,还是“文本”?
  • 我已更改我的答案帖。更改您的表格布局..请将您的表格文本字段更改为 BLOB,尝试使用该配置.. 如果可行,请接受并评价我的回答:) 谢谢!
猜你喜欢
  • 2011-04-14
  • 2011-12-25
  • 2017-03-27
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
相关资源
最近更新 更多