我需要在Blob类型列中插入多行文本数据。
我的桌子是:
CREATE TABLE `messages` (
`id` int(11) NOT NULL,
`msg` longblob NOT NULL
);
另外,我将此文本发布以保存到“ msg”列中:
"This
is
Multiline
Text"
插入部分是;
$mydata->mesaj = "This
is
Multiline
Text";
$sql = "insert into messages(msg) values (:mesaj)";
$stmt = $this->dbConnection->prepare($sql);
$stmt->bindParam(':mesaj', $mydata->mesaj, PDO::PARAM_STR); /* Is it true for use PARAM_STR for longBlob type? */
try {
$stmt->execute();
} catch (PDOException $e) {
//..blah error blah
}
它确实插入带有换行符的文本。当我尝试打开Blob记录时,它显示:
ThisisMultilineText
我尝试将\ n替换为\ n。不幸的是,这也是另一次失败的尝试。需要一个想法。我需要保存多行文本。谢谢, 丹尼兹
答案 0 :(得分:0)
我认为问题在于您用来显示文本的内容。在使用PHP7和MySQL5.6的服务器上,我创建了您的表,然后运行以下代码(与您的代码基本相同,但变量名稍有更改):
echo "<pre>";
$mesaj = "This
is
Multiline
Text";
$sql = "insert into messages(msg) values (:mesaj)";
$stmt = $link->prepare($sql);
$stmt->bindParam(':mesaj', $mesaj, PDO::PARAM_STR); /* Is it true for use PARAM_STR for longBlob type? */
try {
$stmt->execute();
} catch (PDOException $e) {
//..blah error blah
}
$result = $link->query('SELECT msg FROM messages WHERE id = 1');
if ($result) $row = $result->fetch();
echo $row['msg'] . "\n";
echo addcslashes($row['msg'], "\r\n");
我得到的输出是(如预期的那样):
This
is
Multiline
Text
This\r\nis\r\nMultiline\r\nText