Helpful Information
 
 
Category: MySQL Help
Apostrophes and MySQL

Hello everybody !

I'm a french developer and I've got a litlle problem with MySQL.

I need to write a script with PHP3 and MySQL.
I must record a message written by visitor in a MySQL database (it's a e-card service). It seems not to be very difficult. But in french, we are using apostrophes in our textes (e.g. je t'écris ) and when the server calls MYSQL by the way of "insert into ... ", MYSQL interprets the apostrophes in the text as a command-separator and it returns an error.

I don't know if there is a solution for this problem ...
Could someone help me ?
Thanks a lot !

Greg from Paris

Well, I forgot :
I know I can use the escape command
but for the visitors, it's not easy to post messages with before each apostrophe ...

What are you using to insert the data, PHP? Perl? You can always use a regular expression to insert the but there's probably a simpler solution depending on what scripting language you are using.

I'm using PHP 3.

Good, that make's it easy http://www.devshed.com/Talk/Forums/wink.gif

Just use addslashes()

$text=addslashes($text);

(where $text is the textbox input)

This will add slashes to all escapable characters (e.g. ',",)

Rod

What if you're using Perl?

will this do?

$text =~ s/'/'/g;

Thanks

make that $text =~ s/'/'/g
for obvious reasons.

When you insert into mysql you can also use '' (that's two apostropes together '' not ") to represent a single apostrope, this stops you having to write ugly code like s/'/\\\\\\\\\\\\\\'/g;,


Use the regexp s/'/''/g;

It's better to use placeholders. To use Hero's example from the Perl forum:


$dbh->do("DELETE FROM ident WHERE nick =?",undef,($nick));

The $nick variable is escaped automatically.










privacy (GDPR)