Solved

replace one single quote with two single quote

Posted on 2010-11-29
5
275 Views
Last Modified: 2012-08-13
$x='mary's address'

want to replace ' (single quote)
with ''(two single quote)
so I can add to database
0
Comment
Question by:rgb192
  • 3
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34232299
$x = mysql_real_escape_string($x);

$sql = "INSERT INTO mytable ( name ) VALUES ( '$x')";
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34232316
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
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34232406
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34232445
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
 

Author Closing Comment

by:rgb192
ID: 34234496
the escape command caused empty input

the '' (2 single quote) command
worked for me
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now