doutuo6689 2018-08-09 19:43
浏览 77
已采纳

如何使用PDO绑定参数方法将多行文本插入mysql?

I need to insert a multiline text data to Blob type column.

My table is:

CREATE TABLE `messages` (
  `id` int(11) NOT NULL,
  `msg` longblob NOT NULL
);

Addionally I post this text for saving into 'msg' column:

"This
is
Multiline
Text"

Inserting part is;

$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
}

It does insert the text with out newlines. When i try to open blob record, it shows:

ThisisMultilineText

I try replace with . Unfortunetely It was another unsuccesfull attempt too. Need an idea. i need to save with multiline text. Thank you, Deniz

  • 写回答

1条回答 默认 最新

  • dongzuan4917 2018-08-09 23:46
    关注

    I think the problem is in what you are using to display the text. On my server with PHP7 and MySQL5.6 I created your table and then ran the following code (essentially the same as yours but with some minor variable name changes):

    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'] . "
    ";
    echo addcslashes($row['msg'], "
    ");
    

    And the output I get is (as expected):

    This
    is
    Multiline
    Text
    This
    is
    Multiline
    Text
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题