0

问题

SELECT使用PHP 查询在 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. 编码。我将页面编码设置为utf-8使用header('Content-Type: text/html; charset=utf-8');并运行查询mysqli_query($conn,"SET NAMES 'utf8'");mysqli_query($conn,'SET CHARACTER SET utf8');这清除了???????Ùؤتا 呈现而不是阿拉伯语单词的问题。那是一个不同的问题。资源。来源 2。
  2. 数据库字符集。我的数据库和列设置为 UTF-8。

  3. 其他客户工作。SQLBuddy/MySQL 本机客户端/PHPMyAdmin 似乎正在工作,因为运行相同的确切查询会产生结果。因此,我似乎和他在同一条该死的船上。该查询SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى'在 SQLbuddy 上返回结果,但在 PHP 上返回 nada。

可能的解决方案:

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

但是,使用UTF-8编码版本的阿拉伯语单词运行查询会返回 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

输入将不胜感激。

4

1 回答 1

2

使用 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";
    }
}

?>
于 2016-05-27T07:43:48.520 回答