MySQL SELECT 在阿拉伯语单词 returns 上 PHP 有 0 个结果但在 SQLBuddy/phpMyAdmin 上有

MySQL SELECT on Arabic Word returns 0 results on PHP but does on SQLBuddy/phpMyAdmin

问题

使用 PHP 的 SELECT 查询在 MySQL 数据库中搜索阿拉伯语单词时,我得到 0 个结果。

但是,相同的查询会在其他客户端(即 SQLBuddy 等)中产生结果。一切都以 UTF-8 编码。

代码

<?php
    $host = "localhost";
    $username = "hans_wehr_client";
    // i know my security is a joke :)
    $password = "hans_wehr"; 
    $database = "hans_wehr";
    $conn = new mysqli($host, $username, $password, $database);
    if ($conn == TRUE){
        $search = $_GET["search"];
        $encoded_search = utf8_encode($search);
        echo $encoded_search."<br>";
        header('Content-Type: text/html; charset=utf-8');
        $sql = "SELECT * FROM dictionary WHERE ARABIC LIKE '$search'";
        echo $sql."<br>";
        mysqli_query($conn,"SET NAMES 'utf8'");
        mysqli_query($conn,'SET CHARACTER SET utf8');
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0) {
        // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                header('Content-Type: text/html; charset=utf-8');
                echo $row["ARABIC"]. " - Meaning: " . $row["ENGLISH1"]. " " . $row["ENGLISH2"]. "<br>";
            }
        }else {
            echo "0 results";
        }
    }

?>

在模组得到干草叉之前,我必须理清我的故障排除逻辑。

  1. Encoding. 我将页面编码设置为 utf-8 使用 header('Content-Type: text/html; charset=utf-8'); 和 运行 查询 mysqli_query($conn,"SET NAMES 'utf8'");mysqli_query($conn,'SET CHARACTER SET utf8');,这清除了 ???????ÙØ¤ØªØ§ 呈现而不是阿拉伯语单词问题。这是一种不同的 问题。 Source. and Source2.
  2. 数据库字符集。 我的数据库和列设置为 UTF-8。

  3. 其他客户端工作。 SQLBuddy/MySQL 本机客户端/ PHPMyAdmin 似乎在工作,因为 运行 相同精确查询产生结果。因此,我似乎与 him. 在同一条血腥的船上 查询 SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' returns 在 SQLbuddy 上有结果,但在 PHP.

    [=78= 上没有结果]

可能的解决方案:

运行 查询 SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' 给我一个结果。

但是 运行 使用 UTF-8 编码版本的阿拉伯语单词 returns 的查询结果为 0。 SELECT * FROM dictionary WHERE ARABIC LIKE '&#1570;&#1582;&#1614;&#1585;&#1548; &#1571;&#1615;&#1582;&#1585;&#1609;' 我认为这模拟了 PHP。

UTF-8阿拉伯语单词版本是通过解码自动URL编码的$[_GET]参数获得的,即%26%231570%3B%26%231582%3B%26%231614%3B%26%231585%3B%26%231548%3B+%26%231571%3B%26%231615%3B%26%231582%3B%26%231585%3B%26%231609%3B

难道 MySQLi 实际上查询的是 UTF-8 version 而不是实际的阿拉伯语单词?因此找不到匹配项,因为它们不同?

如果是这样,我如何明确告诉 PHP 不要 URL 编码 我的搜索词,然后按原样传递它?

因为根据我的锡纸理论,http://localhost/hans_wehr/search_ar.php?search=آخَر، أُخرى 可以,但是 http://localhost/hans_wehr/search_ar.php?search=%26%231570%3B%26%231582%3B%26%231614%3B%26%231585%3B%26%231548%3B+%26%231571%3B%26%231615%3B%26%231582%3B%26%231585%3B%26%231609%3B

我们将不胜感激。

使用html_entity_decode():

对你的 $_GET["search"] 值使用 html_entity_decode()

<?php
$host = "localhost";
$username = "hans_wehr_client";
// i know my security is a joke :)
$password = "hans_wehr"; 
$database = "hans_wehr";
$conn = new mysqli($host, $username, $password, $database);
if ($conn == TRUE){
    $search = $_GET["search"];
    $encoded_search =html_entity_decode($search, ENT_COMPAT, 'UTF-8');
    echo $encoded_search."<br>";
    header('Content-Type: text/html; charset=utf-8');
    $sql = "SELECT * FROM dictionary WHERE ARABIC LIKE '$encoded_search'";
    echo $sql."<br>";
    mysqli_query($conn,"SET NAMES 'utf8'");
    mysqli_query($conn,'SET CHARACTER SET utf8');
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
    // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            header('Content-Type: text/html; charset=utf-8');
            echo $row["ARABIC"]. " - Meaning: " . $row["ENGLISH1"]. " " . $row["ENGLISH2"]. "<br>";
        }
    }else {
        echo "0 results";
    }
}

?>