简体   繁体   中英

Ajax and MySQL take along time

I have two PHP pages and one JavaScript page
database has one row and 2 columns

main PHP code

<html>
<head>
<title>Hello!</title>
<script type="text/javascript" src="script/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="script/agent.js"></script>
<link rel="stylesheet" href="css/sheet.css" media="screen"/>
</head>
<body>
<table>
<tr>
<td>agent no. :</td>
<td>
<input type="text" class='agent' name="agent"/>
</td>
<td id='agentname'>agent name</td>
</tr>
</table>
</body>
</html>

and agent.php code

<?php
$c=mysql_connect("localhost","root","") or die (mysql_error());
$d=mysql_select_db("davidmag_ac") or die(mysql_error());
if(isset($_GET['agent']) and $_GET['agent']<>""){
$agent=$_GET['agent'];
$result= mysql_query("
select agentname 
from agent 
where agentid=$agent
") or die(mysql_error());
$n=mysql_fetch_assoc($result);
echo $n['agentname'] ;
}
?>

and agent.js code

$(document).ready(function(){
$(".agent").keyup(function(){
agent=$(".agent").val();
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","agent.php?agent="+agent,false);
xmlhttp.send();
document.getElementById("agentname").innerHTML=xmlhttp.responseText;
});
});

The problem is to load agent name take more than one second (1.3 or 1.2 ) every time I press key so if I write 12345 it takes 6 second to load when there is one row
while in phpmyadmin when I try this query take
( 1 total, Query took 0.0004 sec)
so there are more than second waste every time I press key.
I really wonder why?

Ignoring the obvious SQL Injection vulnerabilities, try using mysql_pconnect(), which will keep the connection open:

http://php.net/manual/en/function.mysql-pconnect.php

Be aware that mysql_* functions are depreciated. You should consider switching to either MySQLi or PDO.

我不是一个PHP专家,但我想在每个keyup上建立一个新的数据库连接会减慢速度。

First, it looks like you are developing on a local machine so for a query to take over 1 second means that something else is wrong if it is a small amount of data to parse.

You can help by learning about SQL Indexes and making sure your DB is optimized.

Second, you can convert to PDO and Prepared Statements. They are safer and can be a little faster if the database engine uses persisted connections.

Read about them here: http://php.net/manual/en/book.pdo.php

Also, you NEED to convert to PDO. The mysql_ operations you are using are deprecated and won't exist some day coming soon.

Lastly, change your onKeyUp event to onBlur. You are firing your query with every keystroke. Type in 3 characters and that sends your AJAX request 3 times and queries the database 3 times in less than a second.

It's because you are using keyup function, I would recommend 2 more better options:

The first is $.widget("ui.onDelayedKeyup", {

The second is $('.agent').focusout(function() {

I prefer the second one, because it will send Ajax request after you focus out.

The 1 over seconds taken also involves the http and php overheads. So that is about right.

What i recommend is not to call the ajax page directly with each key press, but to instead set a time out if half a second before calling.

If within that half second, there is another request to trigger the ajax, then skip the current one and let the new request go through.

Use the settimeout function in javascript and store the returned value do that you can check if there is one pending. You can then stop the previous one with that same value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM