dsizmmwnm56437180 2017-04-11 06:01
浏览 6

我在写作的时候如何从数据库中获取结果?

How do we fetch results from a database at the same time we are writing?

Let's say I type in a name field Ja and then a select box drops down fetching all the names in the database that starts with it.

Like:

James
Jay
Jared

and then I type one more letter like Jam and then all the names disappear leaving only James.

What is the name of this process and how do we do it? I'm using PHP only (PDO), do I have to use Java/jQuery too?

  • 写回答

1条回答 默认 最新

  • dongxieyou3314 2017-04-11 06:13
    关注

    The feature that you are trying to build is called autocomplete.

    You will need to write a PHP script (assuming that you are writing a pure PHP based web application) which takes input the typed string in the input box and fire a SQL 'LIKE' query on your database tables and return the list of matched results.

    On the user interface with every keystroke event, you need to make an ajax call which will invoke the aforementioned PHP script and fetch the list of names. You can now display that list in a drop down or there are lot of plugin in jQuery UI which will create this component for you.

    Note: Using LIKE query for autocompleting the strings can be costly if your table contains thousands of rows. If performance is an issue here as well then look into Apache Solr or Elasticsearch to index your data and provide more sophisticated autocomplete results. In that case, instead of the database, you have to query these engines and get the results.

    However, if you are not dealing with high volume of data you can still work with LIKE query.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?