MySQL-自动完成 [英] MySql - autocomplete

查看:88
本文介绍了MySQL-自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Ajax自动完成应用程序,并且想知道是否存在可以用于此目的的SQL查询-例如,如果有人键入"p",我想检索所有以"p"开头的单词(如果添加) "e"检索所有以"pe"开头的单词-并以此继续.

I am creating an Ajax autocomplete application and would like know if there is an SQL query I can use for this purpose - e.g if someone types "p" I would like to retrieve all words beginning with "p", if they add "e" retrieve all words starting with "pe" - and continue like that.

有人建议以下查询,但我认为这不是我想要的:

Someone suggested the query below but I don't think it's what I'm looking for:

$query = "SELECT* FROM nametable WHERE names LIKE '$partialstring' ";

推荐答案

$query = "SELECT* FROM nametable WHERE names LIKE '$partialstring%' ";

我只在右侧添加了%,因为您只希望以输入字母开头的单词.

I've added % only on the right side since you would like to have only the words that are beginning with the input letters.

但是,在针对SQL注入过滤了该查询之前,请不要使用此查询.

However, please don't use this query until you've filtered it against SQL injections.

这篇关于MySQL-自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆