Best way to write MYSQL statement in PHP when data contain's apostrophe.

I have my own way of doing this, but I'm not sure it's best. I would like an expert opinion.

In php, I have a query statement like that is created and then sent to MySQL for processing (I have a function that does all my processing). It reads the data from table A and inserts iinto table B.

First Record
A = 101
B = Tom
C = Smith

Second Record
A = 100
B = Frank
C = O'Connor

$query = "INSERT INTO table (field1, field2, field3) VALUES ( ".$A.", '".$B."', '".$C."')";

$C =str_replace("'", "'", $C

Open in new window

When it reads the first record, the query works fine.
But when it reads the second entry, it crashes on the apostrophe between O and Connor.

I don't want to use "add slashes" because then it puts an unnecessary slash in the database.

I can use str_replace and replace all of the apostrophes with the ascii character (e.g. see line 3 in code above) but I think there must be a better way...

Your insight please...
Paul KonstanskiProject SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Salah Eddine ELMRABETTechnical Lead Manager (Owner)Commented:

Move line 3 before line 1, and remove apostrophe in orther values (make it like the first one: ".$A.")


Nathan RileyFounderCommented:
$query = "INSERT INTO table (field1, field2, field3) VALUES ( '$A','$B','$C')";

Open in new window

I don't want to use "add slashes" because then it puts an unnecessary slash in the database.

No, it doesn't. A slash is an escaping character, meaning that MySQL will not actually insert that slash - it will simply process it and understand that the NEXT apostrophe is not the end of the value, but rather part of the data.

That said, addslashes is probably not the best function to use. Use mysqli_real_escape_string on the value and it'll add all the appropriate escape characters as necessary.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Or if you're not yet on mysqli (you should be by now), mysql_real_escape_string.
The difference between addslashes and the escape string functions may not be obvious at first, but there are other characters that need to be escaped per MySQL's expectations, and addslashes may add slashes that are unnecessary. The escape string functions will do everything that is necessary, EVERY time, so you can simply run that function on each value before you use it and you should never have a problem with the data value interrupting the query.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
A couple of articles that can help you get a little better understanding of these things:

See "When Quotes or Apostrophes are Part of the Data (SQL Queries)" here:

See the examples here:
Chris StanyonWebDevCommented:
Firstly, as gr8gonzo says, if you're using the mysql_* extension, bin it - it's deprecated.

Switch to PDO, use prepared statements and all of this is taken care of:

$stmt = $dbh->prepare("INSERT INTO table (field1, field2, field3) VALUES (?, ?, ?)");
$stmt->execute(array($A, $B, $C));

Open in new window

Job done!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.