How to check Apostrophe with where clause using my

2019-06-04 03:40发布

问题:

How is an apostrophe (') used with the WHERE clause in MySQL?

I have used mysql_real_escape_string to sanitise input already.

Here is a screenshot of my mySQL query and database

SELECT * FROM players WHERE `name` = 'Amar'e Stoudemire'

Executing this query generates the following error:

Any idea why?

回答1:

SELECT * FROM players WHERE `name` = 'Amar\\''e Stoudemire'

If your cell content is \' then you have to escape both characters:

\ --> \\
' --> ''



回答2:

I Found my answer thanx for juergen d who help me out to solve this query

<?php 
$name = "Amar'e Stoudemire";
?>

This is the string with Apostrophe and if you want to check this string in mysql Query with where clause so here is the solution

<?php 
$name = addslashes(addslashes($name));
/*Amar\\\'e Stoudemire*/
?> 

SELECT * FROM players WHERE `name` = "'.$name.'"


回答3:

Single quotes can be escaped with themselves in MySQL - like so:

SELECT * FROM players WHERE `name` = 'Amar\''e Stoudemire'