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?
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:
Hi,

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

Regards.

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

Open in new window

0
gr8gonzoConsultantCommented:
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.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

gr8gonzoConsultantCommented:
Or if you're not yet on mysqli (you should be by now), mysql_real_escape_string.

http://php.net/manual/en/mysqli.real-escape-string.php
http://php.net/mysql-real-escape-string
0
gr8gonzoConsultantCommented:
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.
0

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:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html

See the examples here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
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!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.