0

In my database, I have a column named storeName with a value called Joe's Kitchen.

When user enters Joe's Kitchen, I would store it in a variable named storeName and do a select query on it like this: "SELECT * FROM shops WHERE storename='".$storeName."'". Problem now is that the value contains apostrophe, how should I go about this ?

I have tried the method below but it is not working

$storeName = mysqli_real_escape_string($db->getConnection(),$_POST["storeName"]);
VGG123
  • 9
  • 2

3 Answers3

0

Escape the apostrophe in query by writing two apostrophes
Example


    SELECT * FROM shops WHERE storename='Joe''s Kitchen'  //added 2 apostrophes

this is not a recommended method since it has serious security issues, try to use pdo or parameterized queries
Unni Babu
  • 1,839
  • 12
  • 16
0

In your SQL query, you can replace the single quote ' by `. Then the name can contain single quotes...

Nowhere man
  • 5,007
  • 3
  • 28
  • 44
0

You can do this way also

SELECT * FROM shops WHERE    
storename="Joe\'s Kitchen"
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103