title: Save textarea to MySQL and preserve line breaks
body: <p>Imagine a blog or cms system (PHP and MySQL). I want to let the user enter some text in a textarea and save it to the database. The type of the field in the database is TEXT. </p>
  
  <p>I want to preserve line breaks and print them later. I know I can do this with PHP's <code>nl2br</code>-function, but how do I protect this string against SQL-injection attacks (let's assume I can't use prepared statements). If I use <code>mysql_real_escape_string</code> on it, it does not show me line breaks anymore.</p>
  
  <pre><code>$text = 'one line
  another line';
  $text = mysql_real_escape_string($text);
  /* save to db, fetch it some time later */
  echo nl2br($text); /* output: one line\r\nanotherline */
  </code></pre>