Solved

replace one single quote with two single quote

Posted on 2010-11-29
5
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 110

Expert Comment

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

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

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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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