jquery php 实时搜索

发布于 2024-10-25 02:46:01 字数 1027 浏览 2 评论 0原文

进行实时搜索

我已经使用 ajax 和 php JQUERY

$("#search").keyup(function()   {
   var search_input = $(this).val();
   var dataString = 'keyword='+ search_input;
      if(search_input.length>1){
    $.ajax({
      type: "GET",
      url: "include/search.php?diary_date",
      data: dataString,
      beforeSend:  function() {
             $('#loading').addClass('loading');
          }, success: function(server_response) {                 
               $('#searchresultdata').html(server_response).show();
           $('span#faq_category_title').html(search_input);
    }
});
}return false;
});

: PHP:

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name LIKE '%$keyword%' 
OR l_name LIKE '%$keyword%'
OR email LIKE '%$keyword%'
OR postcode LIKE '%$keyword%'";

我想要搜索结果,从第一个字母搜索,而不是从中间或最后一个字母搜索,例如:
关键字 = JO
我想要的结果=
* 约翰·贝克斯
*乔纳森·威尔科 * KATY JOANS

一会儿系统不仅拿起了第一甚至中间&最后的话
* 托尼·内舍
* 巴乔兹·扎克斯

ive got working live search with ajax and php

JQUERY:

$("#search").keyup(function()   {
   var search_input = $(this).val();
   var dataString = 'keyword='+ search_input;
      if(search_input.length>1){
    $.ajax({
      type: "GET",
      url: "include/search.php?diary_date",
      data: dataString,
      beforeSend:  function() {
             $('#loading').addClass('loading');
          }, success: function(server_response) {                 
               $('#searchresultdata').html(server_response).show();
           $('span#faq_category_title').html(search_input);
    }
});
}return false;
});

PHP:

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name LIKE '%$keyword%' 
OR l_name LIKE '%$keyword%'
OR email LIKE '%$keyword%'
OR postcode LIKE '%$keyword%'";

i want the search result, search from the first letter not from the middle or last for example:
keyword = JO
Result i want =
* JOHN BECKS
* JONATHAN WILKO
* KATY JOANS

for a moment the system pick up not only first even middle & last words
* TONY NESJO
* BAJOZ ZACKS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-11-01 02:46:01

只需从每个 LIKE 字符串中删除第一个 % 即可。

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name LIKE '$keyword%' 
OR l_name LIKE '$keyword%'
OR email LIKE '$keyword%'
OR postcode LIKE '$keyword%'";

也就是说,您应该真的真的调查使用 PDO 或类似的方法并发送绑定查询,其中每个参数都得到正确的转义以防止 SQL 注入攻击。

但是,如果这样做,SQL 会变得稍微复杂一些,因为您无法在字符串内绑定参数,因此必须将 % 添加为 SQL 连接:(

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name LIKE CONCAT(?, '%')
OR l_name LIKE CONCAT(?, '%')
OR email LIKE CONCAT(?, '%')
OR postcode LIKE CONCAT(?, '%')";

然后生成准备好的语句句柄并使用适当的参数执行 - 您会在 Stackoverflow 上找到大量示例)。

Just remove the first % from each of your LIKE strings.

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name LIKE '$keyword%' 
OR l_name LIKE '$keyword%'
OR email LIKE '$keyword%'
OR postcode LIKE '$keyword%'";

That said, you should really, really investigate using PDO or similar and sending a bound query, where each parameter gets properly escaped to prevent SQL injection attacks.

If you do that the SQL gets slightly more complicated, though, because you can't bind a parameter inside a string, so the addition of the % has to be done as a SQL concatenation:

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name LIKE CONCAT(?, '%')
OR l_name LIKE CONCAT(?, '%')
OR email LIKE CONCAT(?, '%')
OR postcode LIKE CONCAT(?, '%')";

(then produce a prepared statement handle and execute with the appropriate parameters - you'll find plenty of examples else where on Stackoverflow).

岁月苍老的讽刺 2024-11-01 02:46:01

您可以使用 REGEXP 运算符,如下所示:

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name REGEXP '^$keyword' 
OR l_name REGEXP '^$keyword'
OR email REGEXP '^$keyword'
OR postcode REGEXP '^$keyword'";

You can use REGEXP operator like so:

$q = "SELECT id_user, f_name, l_name, postcode, email,telp FROM user 
WHERE f_name REGEXP '^$keyword' 
OR l_name REGEXP '^$keyword'
OR email REGEXP '^$keyword'
OR postcode REGEXP '^$keyword'";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文