Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

replace one single quote with two single quote

$x='mary's address'

want to replace ' (single quote)
with ''(two single quote)
so I can add to database
0
rgb192
Asked:
rgb192
  • 3
1 Solution
 
Ray PaseurCommented:
$x = mysql_real_escape_string($x);

$sql = "INSERT INTO mytable ( name ) VALUES ( '$x')";
0
 
Ray PaseurCommented:
Man page here:
http://us.php.net/manual/en/function.mysql-real-escape-string.php

The value inside $x contains an apostrophe, so it must be escaped for use in queries.  Instead of trying to replace just the apostrophe, it's always wisest to use the recommended PHP function that handles all the necessary escape sequences.

HTH, ~Ray
0
 
cyberkiwiCommented:
Just for single quotes to two
$x='mary\'s address';
$x = str_replace("'", "''", $x);

Open in new window

But for insertion to DB, Ray's comment on using mysql escape is gold
0
 
Ray PaseurCommented:
On other thought... If you're working with data bases, you might want to post your questions in both the PHP and the PHP and Databases Zones.  You can use up to three Zones for any question.  If you add the MySQL Server Zone to those other two, you will get more expert eyes on your questions.

Best regards, over and out, ~Ray
0
 
rgb192Author Commented:
the escape command caused empty input

the '' (2 single quote) command
worked for me
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now