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
  • 1

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,819
  • 11
  • 16
0

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

Nowhere man
  • 4,865
  • 3
  • 29
  • 40
0

You can do this way also

SELECT * FROM shops WHERE    
storename="Joe\'s Kitchen"
Always Sunny
  • 32,751
  • 7
  • 52
  • 86