Apostrophe in MySQL
Posted: Tue Jan 27, 2009 11:02 am
When inserting information into a MySQL database via PHP I have had it where when there is an apostrophe, I have to escape it by making it double apostrophe. Below is an example of the code I use:
$message = "5'00";
$message = str_replace("'", "''", $message);
mysql_query("INSERT INTO dbtable (field) VALUES('".$message."')");
And that works fine for most hosts. But, there are some hosts where if I have 2 apostrophes it doesn't work. For those hosts, they don't have a problem with the apostrophe and the code I use looks like the example below:
$message = "5'00";
// $message = str_replace("'", "''", $message);
mysql_query("INSERT INTO dbtable (field) VALUES('".$message."')");
So, this has been my problem, as the code I write works on 1 host but not another. I've been writing scripts that are installed on several hosts, and I need a solution where the code I write works on every host. Is there an "apostrophe solution" so that hosts that require the double apostrophe are happy and hosts that don't need it also work?
THANKS!!!!!!!!!!!!!!!!!!!!!!
Chris
$message = "5'00";
$message = str_replace("'", "''", $message);
mysql_query("INSERT INTO dbtable (field) VALUES('".$message."')");
And that works fine for most hosts. But, there are some hosts where if I have 2 apostrophes it doesn't work. For those hosts, they don't have a problem with the apostrophe and the code I use looks like the example below:
$message = "5'00";
// $message = str_replace("'", "''", $message);
mysql_query("INSERT INTO dbtable (field) VALUES('".$message."')");
So, this has been my problem, as the code I write works on 1 host but not another. I've been writing scripts that are installed on several hosts, and I need a solution where the code I write works on every host. Is there an "apostrophe solution" so that hosts that require the double apostrophe are happy and hosts that don't need it also work?
THANKS!!!!!!!!!!!!!!!!!!!!!!
Chris