title: Preserving double and single quotes and escape characters when inserting in mysql
body: <p>I have a problem when inserting into mysql.</p>
  
  <p>I have a multidimensional array in javascript that gets converted into a string in JSON format.
  The string looks like this for example:</p>
  
  <p><code>[["text1","text2","te'xt with apostroph'e"],["subarray2","a \u000a unicode number","change line \n \n"]]</code></p>
  
  <p>So the thing is that this format uses double quotes outside the fields, and then inside sometimes I have single quotes, and escape characters <code>\</code> as you can see.</p>
  
  <p>If I send this from javascript to php through ajax and decode it in php, it all works great, no problems, perfect.</p>
  
  <p>The problem comes when I need to insert it into mysql.</p>
  
  <ol>
  <li>magic_quotes is off everywhere in php.ini</li>
  <li>I do use mysql_real_escape_string function before I insert in mysql</li>
  </ol>
  
  <p>But whatever I do, the insert is successful, but in the database the double quotes get converted to single quotes, and also the <code>\</code> escape characters disappear.</p>
  
  <p>So inside database I am left with this:</p>
  
  <p><code>[['text1','text2','te'xt with apostroph'e'],['subarray2','a u000a unicode number','change line n n']]</code></p>
  
  <p>This is a problem of course as I need to preserve the double quotes so that when I read this from php later, I can decode the JSON and still keep the single quotes inside the text.  Also the <code>\n</code> new line and the <code>\u000a</code> unicode characters dont work anymore because the escape characters have disappeared.</p>
  
  <p>I found the trick to use <code>htmlspecialchars($thephrase, ENT_QUOTES);</code>
  which converts double quotes to <code>&amp;quot</code>. This at least preserves my double quotes and single quotes separated, but this is obviously not the ideal solution, plus I still lose the escape <code>\</code> characters.</p>
  
  <p>Something is obviously wrong in what I'm doing or the configuration, the problem is not related to the JSON, if I simply insert directly in mysql double and single quotes together, the double ones get converted to single.</p>
  
  <p>So anyway the question I guess is, in a text where I have double quotes, single quotes, escape characters and new line characters, how can I make sure that, when I insert all that in mysql, I preserve all of those in the mysql varchar or text field; double quotes, single quotes, <code>\</code> escape characters and new line characters?</p>
  
  <p>I have made sure that magic_quotes is off everywhere in php.ini</p>
  
  <p>Thank you very much for any advice or help.</p>