jquery php 实时搜索
进行实时搜索
我已经使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需从每个
LIKE
字符串中删除第一个%
即可。也就是说,您应该真的、真的调查使用 PDO 或类似的方法并发送绑定查询,其中每个参数都得到正确的转义以防止 SQL 注入攻击。
但是,如果这样做,SQL 会变得稍微复杂一些,因为您无法在字符串内绑定参数,因此必须将
%
添加为 SQL 连接:(然后生成准备好的语句句柄并使用适当的参数执行 - 您会在 Stackoverflow 上找到大量示例)。
Just remove the first
%
from each of yourLIKE
strings.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:(then produce a prepared statement handle and execute with the appropriate parameters - you'll find plenty of examples else where on Stackoverflow).
您可以使用 REGEXP 运算符,如下所示:
You can use REGEXP operator like so: