How to insert a single quote (‘) into a MySQL database
My computer uses:
MySQL client version: 5.1.41
Server version: 5.1.41
PHP extension: mysqli
The web hosting server uses:
MySQL version 5.0.90-community
cPanel Pro 1.0 (RC1)
On the web hosting server, I can insert a single quote (‘) in a SQL sentence using mysql_query(), but it shows error when I try it on my computer.
I found this method is the best. Before put the string with single quote in a SQL sentence and submit it to the database, replace it with a HTML symbol. An example is as below:
$str = str_replace(‘'’, ‘\”, $str);
When get the string from database, replace the HTML symbol to an equivalent quotation.
$str_re = str_replace(‘'’, ‘\”, $str_from_database);
Another example for quotation marks:
$str = str_replace(‘"’, ‘\”‘, $str);
$str_re = str_replace(‘"’, ‘\”‘, $str_re);