php - 如何在不更改 max_allowed_pa​​cket 的情况下将大数据插入 MySQL 数据库

标签 php mysql pdo insert large-data

我的文本大于 max_allowed_pa​​cket (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 :无法通过引用传递参数 1,但我从以下位置复制了代码: - 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;

最佳答案

像这样尝试:

$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();

还有:

指出:

... binds the LOB into the variable named $lob and then sends it to the browser using fpassthru(). Since the LOB is represented as a stream, functions such as fgets(), fread() and stream_get_contents() can be used on it.

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

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

关于php - 如何在不更改 max_allowed_pa​​cket 的情况下将大数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12898867/

相关文章:

php - 使用 smarty 对数组进行排序

php - 从数据库检索图像 - 图像未显示

mysql - 跨多个表求和值

php - 使用 PDO 调用带有 Out 参数的存储过程

php - PHP 版本之间的不同 PDO 行为

PHPBB 主页集成?

php - 从同一数据集执行2条sql语句

MySQL VARCHAR(255) UTF8 对于 key 来说太长,但最大长度为 1000 字节

mysql - 这是 MySQL 错误还是我的查询错误?

php - 提交SQL查询时是否需要验证列名?