SQL查询在PHP中返回空结果 - 同一查询在PHPMyAdmin中返回结果

问题描述:

I have a webpage which executes several MySQL queries using PHP and returns the results. One query contains a special character - Î - as in ...WHERE region="Île-de-France"... When executed by PHP this query executes successfully but returns an empty result set, when it should return a result. In an attempt to debug I used echo on the $sql variable holding the sql query (just before it is executed) and copied the result into PHPMyAdmin. Here the same query returned the result I was expecting.

Why is this query returning nothing when executed by PHP?

Things I have tried based on information I didn't particularly understand:

I used $sql=$db->real_escape_string($sql) before executing the query. This changed the relevant portion of $sql to ...WHERE region=\"Île-de-France\"... and resulted in an error upon execution of the query.

I also tried if($state){$sql=$sql." AND state ='".$db->real_escape_string($state)."'";}. The query now executed successfully but yielded no results.

Sounds like the database connection you are using has a different character encoding, I would check the default encoding and or try setting it explicitly.