本文介绍了在阿拉伯语单词上的MySQL SELECT在PHP上返回0个结果,但在SQLBuddy/phpMyAdmin上返回0个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

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

I am getting 0 results when searching for an Arabic word in a MySQL database using the SELECT query with PHP.

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

However, the same exact query yields results in alternative clients, namely SQLBuddy and the likes. Everything is encoded in 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";
        }
    }

?>

在mod获得干草叉之前,我必须清除故障排除逻辑.

Before the mods get the pitchforks, I have to clear up my troubleshooting logic.

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

  1. Encoding. I set the page encoding to utf-8 using header('Content-Type:text/html; charset=utf-8'); and ran the queries mysqli_query($conn,"SET NAMES 'utf8'"); and mysqli_query($conn,'SET CHARACTER SET utf8');, this cleared up the ??????? and Ùؤتاrendered instead of Arabic words issue. That is kind of a differentissue. Source. and Source2.
  2. Database Charset. My database and columns are set to UTF-8.

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

Other clients work. SQLBuddy/MySQL native client/ PHPMyAdmin appear to be working because running the same exact query yields result. Therefore I appear to be on the same bloody boat with him. The query SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' returns a result on SQLbuddy but nada on PHP.

可能的解决方案:

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

Running the query SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' yields me a result.

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

However running the query with a UTF-8 encoded version of the Arabic word returns 0 results. SELECT * FROM dictionary WHERE ARABIC LIKE '&#1570;&#1582;&#1614;&#1585;&#1548; &#1571;&#1615;&#1582;&#1585;&#1609;' I think this simulates 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

The UTF-8 Arabic word version is obtained by decoding the automatically URL encoded $[_GET] parameter i.e %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而不是实际的阿拉伯语单词?因此,由于它们不同而找不到匹配项吗?

Could it be that the MySQLi actually queries the UTF-8 version instead of the actual Arabic word? Therefore finding no match since they are different?

如果是这样,我如何明确告诉PHP不要对我的搜索词进行 URL编码,从而照原样传递它?

If so how can I explicitly tell PHP not to URL encode my search term and therefore pass it as it is?

根据我的锡箔理论,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

Since according to my tinfoil theory, http://localhost/hans_wehr/search_ar.php?search=آخَر، أُخرى would work but 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()

Use html_entity_decode():

Use html_entity_decode() on your $_GET["search"] value

<?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";
    }
}

?>

这篇关于在阿拉伯语单词上的MySQL SELECT在PHP上返回0个结果,但在SQLBuddy/phpMyAdmin上返回0个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 06:15