plugins - $wpdb->get_results() does not fetch results with unicode 'WHERE' clause

admin2025-01-08  6

I've just a simple query to fetch a students records by his name, written in Bengali.

$student_rcrd = $wpdb->get_results("SELECT * FROM student WHERE name='আবুল কালাম আজাদ'");

Although my database have a record for 'আবুল কালাম আজাদ', the query returns zero row! But instead of this query, it works absolutely fine-

$student_rcrd = $wpdb->get_results("SELECT * FROM student WHERE name='Abul Kalam Azad'");
  • The name field in the database is 'utf8_unicode_ci'

I've just a simple query to fetch a students records by his name, written in Bengali.

$student_rcrd = $wpdb->get_results("SELECT * FROM student WHERE name='আবুল কালাম আজাদ'");

Although my database have a record for 'আবুল কালাম আজাদ', the query returns zero row! But instead of this query, it works absolutely fine-

$student_rcrd = $wpdb->get_results("SELECT * FROM student WHERE name='Abul Kalam Azad'");
  • The name field in the database is 'utf8_unicode_ci'
Share Improve this question asked Sep 6, 2018 at 3:52 Tajin Ahmed NipunTajin Ahmed Nipun 312 bronze badges 8
  • Did you check the SQL directly, like from PHPMyAdmin? – Fayaz Commented Sep 6, 2018 at 5:19
  • Also, if you are using the unicode text directly in the file (not from user input), then make sure the php file itself is saved as unicode UTF8 (without BOM) from your editor. – Fayaz Commented Sep 6, 2018 at 5:21
  • I have checked the SQL from phpMyAdmin and it returns the row. the unicode text is actually inserted by the user. For Simplification I mentioned the query directly. – Tajin Ahmed Nipun Commented Sep 6, 2018 at 5:32
  • 1 Check what charset is defined in wp config file. try setting this define('DB_CHARSET', 'utf8mb4'); or define('DB_CHARSET', 'utf8'); – CodeThing Commented Sep 6, 2018 at 5:34
  • 1 I'm not sure, but it may be a collation issue. Try using: add_filter( 'pre_get_table_charset', '__return_false' ); before the query and see if it works. If it does, then it's a collation issue (don't actually use it on production, this is just for test). – Fayaz Commented Sep 6, 2018 at 5:47
 |  Show 3 more comments

1 Answer 1

Reset to default 0

Your data are possibly corrupt from trying to store multibyte characters that your DB could not correctly represent. MySQL's utf8 collation isn't actually UTF-8. You will have to convert your tables' character sets to utf8mb4 (real UTF-8), and then set the DB_CHARSET constant to match.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268850a1307.html

最新回复(0)