Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apostrophes are breaking my mysql query in PHP

My database has name records that occasionally contain apostrophes, such as Joe's Bar and I've just coded a query script in PHP that grabs that field and sticks it into a select statement with the usual $query = "SELECT address FROM restaurants WHERE name='$name'"; and the apostrophe in some of the restaurant names derails the Love Train.

How do I keep this from happening?

Snide answer - Use the same technique you used when you inserted them INTO the database via PHP.
Rebuttal - I was having the same problem then and cheated and entered the troublesome ones directly using PHPMyAdmin but this can't be ignored any longer.

Thank you for taking the time to answer this during the holidays.

like image 532
Drew Avatar asked Dec 25 '09 15:12

Drew


1 Answers

You have to $name = mysql_real_escape_string($name); before that line.

You might also want to read up on SQL Injections, since your inputs are clearly unsanitized.

like image 118
styts Avatar answered Sep 21 '22 12:09

styts