0

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?

5
  • Already tried that. Same result. Don't understand that... Commented Dec 11, 2014 at 22:42
  • maybe the db is doing some auto-escaping of "\" could you have a look ? Commented Dec 11, 2014 at 22:44
  • Why has this question been downgraded? Commented Dec 11, 2014 at 22:50
  • I didn't downvote but this is the very most basic of PHP and covered extensively in the manual and on SO. Commented Dec 11, 2014 at 22:51
  • Perfectly valid question, and one that seems to come up quite a bit based on the activity on this post: stackoverflow.com/questions/3446216/… Commented Dec 12, 2014 at 4:50

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Wow. Would never think abou that. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.