gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:41:52 25 4
gpt4 key购买 nike

我的文本大于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com