Solved

replace one single quote with two single quote

Posted on 2010-11-29
5
277 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 109

Expert Comment

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

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

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 109

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP not parsing ' character 12 46
Why is my wamp get_include_path() wrong? 2 21
How to obtain the string from a PHP StdObject ? 6 33
Link failure 16 35
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

830 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