在 php 字符串中放置换行符,并在 MySQL-DB 中正确写入所有内容


Put linebreaks in a php-string and write everything correctly in MySQL-DB

我尝试生成一个带有换行符的字符串,我想将其保存在MySQL-DB中

//get some data
while($data = $anything->fetch(PDO::FETCH_OBJ))
    $result .= $data->field.''r'n';
}
$update = $paed_db->prepare('UPDATE table SET anything = :result WHERE id = :id');
$update->bindParam(':id', $id, PDO::PARAM_INT);
$update->bindParam(':result', trim($result), PDO::PARAM_STR);
$update->execute();

但是在文本区域中读取结果后,没有换行符,而是一个看起来像"Lorem''r'ipsum''r'dolor"的字符串。

我也试过

$update->bindParam(':result', trim(htmlspecialchars($result)), PDO::PARAM_STR);

我做错了什么?

使用"'r'n"(双引号),因为转义序列不在单引号内解释:

  • PHP 单引号

  • PHP 双引号