【发布时间】:2010-12-02 00:54:09
【问题描述】:
我正在使用下面的 PHP 代码来获取返回到自动建议/自动完成的结果。问题是,如果较早的 SQL 查询从数据库返回所需的时间比最近的 SQL 查询长,那么旧查询的结果将显示在自动建议结果框中。因此,如果您开始搜索 Thomas,如果该 SQL 查询需要更长的时间,它可能会显示 Tho 的结果。我想知道是否有办法在实施新查询后取消以前的查询,或者确保使用最新的查询?
<?php
// begin XML output
$xmlStr = <<<XML
<?xml version='1.0' standalone='yes'?>
<authors>
XML;
// open database connection
$mysqli = new mysqli("localhost", "user", "pass", "library");
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
// retrieve author list matching input
// add to XML document
$q = $mysqli->real_escape_string($_GET['query']);
$sql = "SELECT AuthorName FROM author WHERE AuthorName LIKE '" . $q . "%' ORDER by AuthorName";
if ($result = $mysqli->query($sql)) {
while ($row = $result->fetch_row()) {
$xmlStr .= '<author name="' . $row[0] . '"></author>';
}
$result->close();
}
// clean up
// output XML document
$mysqli->close();
$xmlStr .= '</authors>';
header("Content-Type: text/xml");
echo $xmlStr;
?>
【问题讨论】:
-
如果这个和Ajax有关,可以加上
ajax标签。
标签: php sql autocomplete yui