我正在做一个程序,要求用户根据onKeyup
从Mysql表中搜索。但似乎响应时间非常缓慢,而我没有那么多数据 ~2873记录。
这是我的PHP代码:
<?php
session_start();
if(isset($_SESSION['master'])){
require_once('connect_db.php');
require_once('output_fns.php');
<div id="openModal6" class="modalDialog6">
<div>
<a href="#close" title="Close" class="close">X</a>
<h2><center>Search Item</center></h2>
<p>
<table>
<tr><td>
<b>Item No. :</b><br />
//User input and onKeyup
<input type="text" value="" id="item_s" autofocus required onKeyUp="related_item(document.getElementById('item_s').value,document.getElementById('store_s').value);">
<td><b>Stor</b><br />
//User select Store by default usually its first store
<select name='store_s' id='store_s'>
<?php
$query = "SELECT * from stores_list";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row = mysql_fetch_assoc($result);
echo "<option value='{$row['short']}'>{$row['short']}</option>";
}
echo "</select>";
}
else{
//No Stores Available
echo "No Stores Found !";
}
?>
</td>
</tr>
<tr><td align="center" colspan="3">
//Response Output Here
<div id="txtHint4">Waiting...</div>
</td></tr>
</table>
</p>
</div>
</div>
<?php
}
当然这是一个模态对话,所以当用户点击|Search Button|
并使用Onclick=
至于Ajax get_item.php
<?php
require_once('connect_db.php');
$q=$_GET['q'];
$s=$_GET['s'];
if($_GET['q']=="---"){
return true;
exit();
}
//Select Result From table where item Like
$query="SELECT * FROM store_items WHERE item_no LIKE '".$q."%' AND store = '".$s."' ORDER by sub Asc";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
echo "<br />";
echo "<table width=100% height=% align=center border=1 bgcolor=white cellpadding=5 cellspacing=0 style='font-size:12px;'>";
echo "<tr><td align=center><b>Item Number</b><td align=center><b>QTY</b><td align=center><b>Unit Cost</b></td><td align=center><b>Unit Sell</b></td><td align=center><b>Cat</b></td><td align=center><b>Sub</b></td><td align=center><b>Description</b></td><td align=center><b>Store</b></td></tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr><td align=center>{$row['item_no']}</td><td align=center>{$row['qty']}<td align=center>{$row['actual_price']}</td><td align=center>{$row['selling_price']}</b></td><td align=center>{$row['cat']}</td><td align=center>{$row['sub']}</td><td align=center>{$row['short']}</td><td align=center>{$row['store']}</td></tr>";
//End of While loop
}
}
else{
echo "<br /><center><font color=red>Sorry Item Number Not Found !</font></center>";
}
?>
最后是功能 related_item
function related_item(str,str2)
{
if (str=="")
{
document.getElementById("txtHint4").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint4").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_item.php?q="+str+"&s="+str2,true);
xmlhttp.send();
}
到目前为止,我做错了什么?它应该从表中快速获取数据,我不明白大约需要4-5秒等待时间来查看<div id='txtHint4'>Response</div>
内的数据
请注意,这些项目只是数字,例如(001200201000)
更新:
我已经检查了Chrome开发人员工具网络以测试延迟部分,似乎延迟发生在get_item.php
文件中这是结果
Query 0.36
Stalled 0.5
Request Sent 0.14ms
Waiting (TTFB) 77.555ms
Content Download 3.40s
那么这意味着我的PHP文件有问题吗?还有什么其他或更好的方法来增强代码?它直接从桌子上读取。也许我在这里遗漏了一些东西