2016-08-18 28 views
0

我已經完成了前面匹配字符的高級搜索。我希望它應該匹配中間和單詞的結尾或者數據庫中的任何地方。我應該怎麼可以d我的高級搜索代碼是在這裏 PHP代碼........................ 字符智能在A​​JAX和PHP中提前搜索?

<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>Untitled Document</title> 
 
<style> 
 
table { 
 
    width: 100%; 
 
    border-collapse: collapse; 
 
} 
 

 
table, td, th { 
 
    border: 1px solid black; 
 
    padding: 5px; 
 
} 
 

 
th {text-align: left;} 
 
</style> 
 
</head> 
 

 
<body> 
 
<?php 
 
\t \t $servername="localhost"; 
 
\t \t $username="root"; 
 
\t \t $password=""; 
 
\t \t $conn=mysql_connect($servername,$username,$password); 
 
\t if (!$conn) { 
 
    \t die('Could not connect: ' . mysqli_error($conn)); 
 
\t } 
 
\t 
 
\t $in=$_GET['q']; 
 
if(!ctype_alnum($in)){ 
 
    echo "Data Error"; 
 
    exit; 
 
} 
 

 
\t \t mysql_select_db('firstdb'); 
 
\t \t $sql="select name, id , age, sex from name where name like '$in%' or sex like '$in%' or age like '$in%' or id like '$in%'"; 
 
\t \t $display=mysql_query($sql,$conn); 
 
\t echo "<table> 
 
\t <tr> 
 
\t <th>ID</th> 
 
\t <th>Name</th> 
 
\t <th>Age</th> 
 
\t <th>Gender</th> 
 
\t </tr>"; 
 
while($row = mysql_fetch_assoc($display)){ 
 
    echo "<tr>"; 
 
    echo "<td>" . $row['id'] . "</td>"; 
 
\t echo "<td>" . $row['name'] . "</td>"; 
 
    echo "<td>" . $row['age'] . "</td>"; 
 
    echo "<td>" . $row['sex'] . "</td>"; 
 
    echo "</tr>"; 
 
} 
 
echo "</table>"; 
 

 
?> 
 
</body> 
 
</html>

JS代碼

<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>Untitled Document</title> 
 
<script> 
 
function user(str){ 
 
    if(str.length==0) 
 
\t { 
 
\t document.getElementById("userhint").innerHTML = ""; 
 
     return; 
 
\t } \t 
 
\t else { 
 
     var xmlhttp = new XMLHttpRequest(); 
 
     xmlhttp.onreadystatechange = function() { 
 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
 
       document.getElementById("userhint").innerHTML = xmlhttp.responseText; 
 
      } 
 
     } 
 
     xmlhttp.open("GET", "advanceSearch.php?q="+str, true); 
 
     xmlhttp.send(); 
 
    } 
 

 
\t 
 
} 
 

 
</script> 
 
</head> 
 
<body> 
 

 
<form> 
 
<input type="text" onkeyup="user(this.value);" name="username" /> 
 
</form> 
 
<br /> 
 
<div id="userhint"><b>User info will be listed here...</b></div> 
 
</body> 
 
</html>

+0

能否請您解釋一下你想更清楚地做什麼? – Praveen

+0

@parveen當我在文本中編寫任何字符時,它應該獲取包含該字符的所有記錄,而不考慮字符位置,但我的上面的查詢只匹配從開始 –

回答

1

一點變化在查詢完成

$sql="select name, id , age, sex from name where name like '%$in%' or sex like '$%in%' or age like '%%$in%' or id like '%$in%'";