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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据