2013-06-03 18 views
-1

我想創建搜索引擎使用PHP從我的數據庫搜索阿拉伯文字母,但我沒有得到任何東西,但我的代碼工作正常與英文字母和數字..... 這裏我的代碼搜索引擎在阿拉伯字母在PHP

<?php 

    if(isset($_POST['submit'])){ 
    if(isset($_GET['go'])){ 
    $name=$_POST['name']; 
    if(empty($name)) { 
        echo"Plz enter the Search"; 
        } 

    if(preg_match("/^[(a-z) (A-Z) (0-9)]+/u", $name)){ 

//connect to the database 
    $db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); 
//select the database to use 
    $mydb=mysql_select_db("student"); 
    mysql_set_charset("utf8"); 
    $sql="SELECT id, fname, lname FROM st_information WHERE id LIKE '%" . $name . "%' OR fname LIKE '%" . $name . "%' OR lname LIKE '%" . $name ."%'"; 
//Run the query against the mysql query function 
    $result=mysql_query($sql); 
//Create while loop and loop through result set 
    while($row=mysql_fetch_array($result)){ 
      $FirstName =$row['fname']; 
      $LastName=$row['lname']; 
      $ID=$row['id']; 
//Display the result of the array 
    echo "<ul>\n"; 
    echo "<li>" . "<a href=\"show.php?id=$ID\">" .$FirstName . " " .$LastName . "</a></li>\n"; 
    echo "</ul>"; 
    } 
    } 

}  
    } 
+0

您的表單是否輸入UTF8? –

+0

此代碼易受SQL注入攻擊 – joschua011

+0

是的,我使用的是標籤,charset = utf8 –

回答

0

/^[(a-z) (A-Z) (0-9)]+/u將不匹配當然任何阿拉伯字符。

報價this answer

preg_match("/\p{Arabic}/u", $string) 

當然,謹防SQL注入,並移動到PDOmysqli如已經指出。

+0

非常感謝moonwave99,它的工作:) –