6

我是网络编程的新手,所以如果我说错了,我深表歉意。我正在编写一个简单的 php 脚本来搜索阿拉伯语 mysql 数据库(来自本网站http://qurandatabase.org/Database.aspx - mysql 查询文件格式 - 阿拉伯语(原始)的古兰经数据库),但我有一个非常简单的问题,这是我在我的 php 脚本中通过 GET 方法收到的不是我输入搜索的内容,并且在我的搜索字符串中添加了一些不需要的阿拉伯字符。我的代码和输出如下:我的 html 代码是:

 <!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml" lang="ar" xml:lang="ar">
     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
     <body>
         <form action="p.php" method="get" accept-charset="UTF-8">
             Word Search: <input type="text" name="word"/>
             <input type="submit"/>
         </form>
     </body>
 </html>

我的PHP代码是:

<?php
 header('Content-type: text/html; charset=utf-8');
 $DBServer="localhost";
 $DBUser="root";
 $DBPass="1";
 $DB="Quran";
 $DBTable="Quran";
 //echo "Welcome".$_GET["fname"];
 $Word=$_GET['word'];
 echo $Word;
 echo "<br />";
 echo "<br />";
 $con=mysql_connect($DBServer,$DBUser,$DBPass);
 if(!$con)
 {
     die("DB Connection error: ".mysql_error());
 }
 mysql_select_db($DB,$con);
 echo "<br />\n";

 echo "<br />\n";
 echo "<br />\n";
 echo "<br />\n";
 echo "select * from $DBTable where AyahText like '%".$Word."%'";
 echo "<br />\n";
 $select="select * from $DBTable where AyahText like '%ﻢِﻧَ ﺎﻠْﺠِﻧَّﺓِ ﻭَﺎﻠﻧَّﺎﺳِ%'";
 echo $select;
 echo "<br />\n";
 echo "<br />\n";
 $select="select * from $DBTable where AyahText like '%".urldecode($Word)."%'";
 echo $select;
 echo "<br />";
 $result=mysql_query($select);
 //$result=mysql_query("select * from $DBTable where VerseID=$Word");
 $LastSura=1;
 echo $LastSura;
 echo "<br />";
 while($row=mysql_fetch_array($result))
 {
     echo "salam";
     echo $row['SuraID']."\t".$row['VerseID']."\t".$row['AyahText'];
     echo "<br />";
     if($row['SuraID']!=$LastSura)
     {
         echo "<br />";
         echo "<br />";
     }
     $LastSura=$row['SuraID'];

 }
 mysql_close($con);
 ?>

我在我的 html 文本框中输入了这个字符串:

مِنَ الْجِنَّةِ وَالنَّاسِ

但我得到这个字符串,输出几乎没有差异,你必须自己测试它:

مِنَ الْجِنَّةِ وَالنَّاسِ

select * from Quran where AyahText like '%مِنَ الْجِنَّةِ وَالنَّاسِ%'

select * from Quran where AyahText like '%مِنَ الْجِنَّةِ وَالنَّاسِ%'

select * from Quran where AyahText like '%مِنَ الْجِنَّةِ وَالنَّاسِ%' 1

它导致我的搜索失败。

我不知道这个用于提问的复制和粘贴是否让我的正确输出正确显示给你,但在我的浏览器中 الْجِنَّةِ 和 النَّاسِ 在 ن 我的输出中有不需要的 ِ 或 َ。非常感谢您的帮助。

4

3 回答 3

3

مِنَ الْجِنَّةِ وَالنَّاسِ 和 مِنَ الْجِنَّةِ وَالنَّاسِ 之间的区别在于不可打印字符位于不同的位置。根据您使用的排序规则,这会使 MySQL 中的字符串相等或不同,从而影响查询结果:

mysql> select 'مِنَ الْجِنَّةِ وَالنَّاسِ' = 'مِنَ الْجِنَّةِ وَالنَّاسِ' collate utf8_general_ci general;
+---------+
| general |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)

mysql> select 'مِنَ الْجِنَّةِ وَالنَّاسِ' = 'مِنَ الْجِنَّةِ وَالنَّاسِ' collate utf8_unicode_ci unicode;
+---------+
| unicode |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

更改为列定义的排序规则以获得所需的结果,例如:

alter MyTable modify MyColumn text collate utf8_general_ci;
于 2012-12-04T15:24:07.670 回答
0

您不需要执行 urldecode()。$_GET当 PHP作为其启动序列的一部分进行填充时,它已经为您完成了这项工作。

请注意,您的代码容易受到 SQL 注入攻击。正确的搜索顺序是:

$word = $_GET['word'];
$safeword = mysql_real_escape_string($word);  
$sql = "SELECT ... WHERE AyahText LIKE '%$safeword%'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($rresult)) {
   ...
}

当然,你会在这里收到很多关于不使用 mysql 库的评论,因为它已被弃用。

于 2012-12-04T14:40:57.707 回答
0

这是我用您的话测试的示例代码,它打印了包含该单词的 suraNo/ayahNo/ayah。

<?php
header('Content-type: text/html; charset=utf-8');
$server="localhost";
$user="root";
$pass="root";
$database="quran";
$table="quran";
$word=$_GET['word'];
$connection=mysql_connect($server,$user,$pass);
if(!$connection)
{
 die("DB Connection error: ".mysql_error());
}
mysql_select_db($database,$connection);
mysql_query("SET NAMES utf8"); //IMPORTANT
?>
<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml" lang="ar" xml:lang="ar">
     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
     <body>
         <form method="get" accept-charset="UTF-8">
             Word Search: <input type="text" name="word"/>
             <input type="submit"/>
         </form>
         <div>
         <?php
            if (!empty($word))
            {
                $i = 0;
                $str = "SELECT * FROM $table WHERE AyahText LIKE '%$word%'";
                $query = mysql_query($str);
                while ($row = mysql_fetch_assoc($query))
                {
                    $i++;
                    echo "<br/>$i: ($row[SuraID], $row[VerseID]): $row[AyahText]";
                }
            }
         ?>
         </div>
     </body>
 </html>

您必须在选择数据库后调用它。当我删除这条线时,我无法得到任何结果。

mysql_query("SET NAMES utf8");
于 2012-12-04T15:16:36.863 回答