简体   繁体   中英

SQL query returns empty result in PHP - same query returns result in 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.

听起来您正在使用的数据库连接具有不同的字符编码,我会检查默认编码或尝试显式设置它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM