简体   繁体   English

加快实时搜索js,php,mysql

[英]Speed up live search js, php, mysql

I have made a "live search bar" with php and javascript. 我用php和javascript制作了一个“实时搜索栏”。 if you enter a word in the search bar it searches the database (mysql). 如果在搜索栏中输入单词,它将搜索数据库(mysql)。

index.php: index.php文件:

<input type="text" onkeyup="getMovie(this.value)"/>
<div id="movie"></div>

javascript.js: javascript.js:

function getMovie(value) {
$.post("getmovie.php",{partialMovie:value},function(data) {
    $("#movie").html(data);
});
}

getmovie.php: getmovie.php:

include_once "connection.php"; 

if($_POST['partialMovie']){
    $partialMovie = $_POST['partialMovie'];

    $sql = "SELECT title FROM movie WHERE title LIKE '%$partialMovie%'";
    $stm = $db->prepare($sql);
    $result = $stm->execute(array());
    while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
        echo "<li>".$row['title']."</li>";
    }
}

This works, but it is way to slow. 这可行,但是它是减慢速度的方法。 I have only 3 "movies" in my database, but it takes like a second or two to show the results. 我的数据库中只有3个“电影”,但是显示结果大约需要一两秒钟。

the titles of the 3 movies are: een twee drie. 这三部电影的标题是:een twee drie。 but if i type "een" fast, after a second you see een, twee, drie. 但是如果我快速输入“ een”,一秒钟后,您会看到een,twee,drie。 a second later you see: een twee. 一秒钟后,您会看到:een twee。 and another second later you see: een. 再过一秒钟,您会看到:een。

So my question is: is there a way to speed the search up or is there a way to stop searching if you type another letter? 所以我的问题是:如果您键入另一个字母,是否可以加快搜索速度或是否可以停止搜索?

Either lower your expectation, because 1 second for a request's round trip is not very improvable, or get the data as json at page load time and search against locally available json data. 降低您的期望值,因为请求的往返行程不是非常快1秒,或者在页面加载时将数据作为json并搜索本地可用的json数据。 But if there are many records this might not be an option. 但是,如果有很多记录,则可能无法选择。

As you use PDO, bind your parameters to avoid sql injection. 使用PDO时,请绑定参数以避免sql注入。

Then use full text search in MySQL (select match against), it will be a lot faster than the like %% approach. 然后在MySQL中使用全文搜索(选择“与”匹配),它将比类似%%的方法快得多。

Doc : http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html Doc: http : //dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

HTML : HTML

<input type="text" id="movie-autocomplete"/>
<div id="movie"></div>

JS : JS

$(document).ready(function(){
    $('#movie-autocomplete').keyup(function(){
        setTimeout(function(){
            $.post(
                "getmovie.php",
                {
                    partialMovie: $(this).val()
                },
                function(data) {
                    $("#movie").html(data);
                }
            );
        }, 500);
    });
});

This will create a small delay and post an atual value within the field. 这将产生一个小的延迟,并在该字段内发布一个标准值。

PHP : PHP的

include_once "connection.php"; 

if(!empty($_POST['partialMovie'])){
    $partialMovie = $_POST['partialMovie'];

    $sql = "SELECT title FROM movie WHERE title LIKE :movie ORDER BY title LIMIT 0, 10";
    $stm = $db->prepare($sql);
    $result = $stm->execute(array(':movie' => "%{$partialMovie}%"));
    while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
        echo "<li>".$row['title']."</li>";
    }
}

We have to bind the parameter to secure the SQL query and prevent SQL injection . 我们必须绑定参数以保护SQL查询并防止SQL注入 Also You should ensure You have set the PDO to use real prepared statements. 另外,您还应确保已将PDO设置为使用实际的准备好的语句。

To further speed all the process You should return only JSON data from the PHP and fill in the <ul> within Your JS code... 为了进一步加快整个过程,您应该只从PHP返回JSON数据,并在JS代码中填写<ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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