【问题标题】:Make sure most recent SQL query result is used in autosuggest确保在自动建议中使用最新的 SQL 查询结果
【发布时间】: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


【解决方案1】:

您可能希望同步 AJAX 调用。就像在按键事件之后有几秒钟的延迟,然后进行 AJAX 调用。设置asynchronous = true 也有帮助。

【讨论】:

    【解决方案2】:

    在发出新请求之前,您可以通过调用 abort 方法来中止 ajax 调用:XmlHttpRequest.abort()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-27
      • 2014-05-06
      • 2015-09-04
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多