我的文本大于 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 :无法通过引用传递参数 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_packet 的情况下将大数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12898867/