user3142695
user3142695

Reputation: 17322

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

I try to generate a string with linebreaks, which I want to save in a 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();

But after reading the result in a textarea, there are no linebreaks but a string which looks like "Lorem\r\nipsum\r\ndolor".

I also tried

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

What am I doing wrong?

Upvotes: 0

Views: 31

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Use "\r\n" (double quotes) since escape sequences aren't interpreted inside of single quotes:

Upvotes: 4

Related Questions