Mysql – Different Result When Insert Data Contain Quote on Localhost and Online MySQL Hosting

MySQLPHP

I installed xampp on localhost and insert data contain quote using this php code

$content    = htmlentities($_POST['content']);
$content_esc = mysql_real_escape_string($content);
$save = mysql_query("INSERT INTO tbl_post (title,content,date,publish) values('$title_esc','$content_esc','$date','$publish')");

The result on localhost website is

this is 'example data'

The row on database filled with this

this is "example data"

But using the same code to insert data the result from my hosting provider is different

this is \'example data\'

The row on database filled with this

this is \"example data\"

I test my website on localhost, insert post contain quote and it show on page correctly. Then i surprise when i upload on online hosting, insert post and the post page show '\' before the quote.

At this time i replace the '\' with nothing. How to insert the quote so it can show same result on local mysql database and online mysql database ? What is wrong, my php code or my mysql database ?

Best Answer

Looks PHP issue, I bet you have Magic Quotes turned off on local (good!) whereas your hosting privider has turned them on - http://www.php.net/manual/en/security.magicquotes.what.php. When they're turned on it's the same like if you'd write:

$content    = htmlentities(addslashes($_POST['content']) );//HERE: addslashes is basically the same like your mysql_real_escape_string() - this means contents of $_POST get escaped automatically
$content_esc = mysql_real_escape_string($content);
$save = mysql_query("INSERT INTO tbl_post (title,content,date,publish) values('$title_esc','$content_esc','$date','$publish')");

I also guess that next time you're better asking such a question on StackOverflow instead;)