Solved

replace one single quote with two single quote

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

615 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