gpt4 book ai didi

php - 使用 PHP 的 mysqli 搜索 mySQL 的特殊 unicode 字符

转载 作者:行者123 更新时间:2023-11-29 09:35:43 24 4
gpt4 key购买 nike

我有一个搜索自动完成功能,当有人输入法语字符(例如 É)时,该功能会中断

É 在 mySQL 表中的存储方式类似于“\u00c9”(unicode 代码点):

'id', 'term', 'count', 'words', 'locale'
'5218', '\u00c9COLORADO', '4', '1', 'fr-ca'
'5590', '\u00c9MADEUP', '1', '1', 'fr-ca'
'5511', 'EXCITE', '1', '1', 'fr-ca'

在 PHP 中,É 是 '\xc3\x89'。我编写了下面的代码,将其转换为 unicode 以进行查询,以便它匹配。在我的系统上,json_encode() 输出“\\u00c9”,所以我必须 str_replace() 一些额外的字符

$andrew = json_encode($criteria);  
$temp2 = str_replace('"', "", $temp1);
$temp3 = str_replace('\\\\', '\\', $temp2);
$data = self::all( array( 'locale' => $locale , 'term' => array('$like' => $temp3."%" ) ), array('count'=>0,'term'=>2),0,12 );

当我在搜索中输入 É 并在 SQL 查询中输入 error_log() 时,它是:

SELECT * FROM search_term WHERE `locale` = 'fr-ca'  AND `term` LIKE '\\\\u00c9%'  ORDER BY `count` DESC, `term` ASC,

当我在 mySQL Workbench 中运行该 SQL 查询时,它可以工作(在 LIKE 的情况下需要四个反斜杠),结果集为:

'id', 'term', 'count', 'words', 'locale'
'5218', '\u00c9COLORADO', '4', '1', 'fr-ca'
'5590', '\u00c9MADEUP', '1', '1', 'fr-ca'

但是当我使用 mysqli 在 PHP 中运行该查询时:

$res = mysqli_query($conn, $query);

它不会返回任何结果/匹配。

mysqli_query() 如何或为何更改查询,导致查询失败?我该如何编写此代码,以便当搜索字符为 É 时,它与该字符匹配 - 它如何存储 - 在数据库中?

最佳答案

json_encode($str, JSON_UNESCAPED_UNICODE)

添加该标志,以便您获得字母,而不是 Unicode 代码。

关于php - 使用 PHP 的 mysqli 搜索 mySQL 的特殊 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com