简体   繁体   English

显示来自 MySQL 和 PHP 的最新搜索结果

[英]Display latest search results from MySQL with PHP

Google unfortunately didn't seem to have the answers I wanted.不幸的是,谷歌似乎没有我想要的答案。 I currently own a small search engine website for specific content using PHP GET.我目前拥有一个使用 PHP GET 获取特定内容的小型搜索引擎网站。

I want to add a latest searches page, meaning to have each search recorded, saved, and then displayed on another page, with the "most searched" at the top, or even the "latest search" at the top.我想添加一个最新搜索页面,这意味着记录、保存每个搜索,然后显示在另一个页面上,顶部是“搜索最多”,甚至顶部是“最新搜索”。

In short: Store my latest searches in a MySQL database (or anything that'll work), and display them on a page afterwards.简而言之:将我的最新搜索存储在 MySQL 数据库(或任何可行的数据库)中,然后在页面上显示它们。

I'm guessing this would best be accomplished with MySQL, and then I'd like to output it in to PHP.我猜这最好用 MySQL 来完成,然后我想把 output 放到 PHP 中。

Any help is greatly appreciated.任何帮助是极大的赞赏。

Recent searches could be abused easily.最近的搜索很容易被滥用。 All I have to do is to go onto your site and search for "your site sucks" or worse and they've essentially defaced your site.我所要做的就是将 go 放到您的网站上,然后搜索“您的网站很烂”或更糟,他们实际上已经破坏了您的网站。 I'd really think about adding that feature.我真的会考虑添加该功能。

In terms of building the most popular searches and scaling it nicely I'd recommend:在构建最受欢迎的搜索并很好地扩展它方面,我建议:

  • Log queries somewhere.在某处记录查询。 Could be a MySQL db table but a logfile would be more sensible as it's a log.可能是 MySQL 数据库表,但日志文件会更明智,因为它是日志。
  • Run a script/job periodically to extract/group data from the log定期运行脚本/作业以从日志中提取/分组数据
  • Have that periodic script job populate some table with the most popular searches让那个定期脚本作业用最流行的搜索填充一些表

I like this approach because:我喜欢这种方法,因为:

  • A backend script does all of the hard work - there's no GROUP BY, etc made by user requests后端脚本完成所有艰苦的工作 - 没有用户请求进行的 GROUP BY 等
  • You can introduce filtering or any other logic to the backend script and it doesn't effect user requests您可以在后端脚本中引入过滤或任何其他逻辑,它不会影响用户请求
  • You don't ever need to put big volumes of data into the database您永远不需要将大量数据放入数据库

Create table (something named like latest_searches ) with fields query , searched_count , results_count .创建带有字段querysearched_countresults_count的表(名为latest_searches的东西)。
Then after each search (if results_count>0), check, if this search query exists in that table.然后在每次搜索之后(如果 results_count>0),检查该搜索查询是否存在于该表中。 And update or insert new line into table.并在表中更新或插入新行。
And on some page you can just use data from this table.在某些页面上,您可以只使用该表中的数据。

It's pretty simple.这很简单。

You simply store the link and name of the link/search in MySQL and then add a timestamp to record what time sb searched for them.您只需将链接/搜索的链接和名称存储在 MySQL 中,然后添加时间戳以记录 sb 搜索它们的时间。 Then you pull them out of the DB ordered by the timestamp and display them on the website with PHP.然后,您将它们从按时间戳排序的数据库中拉出,并使用 PHP 将它们显示在网站上。

  1. Create a table with three rows: search link timestamp.创建一个包含三行的表:搜索链接时间戳。
  2. Then write a PHP script to insert rows when needed (this is done when the user actually searches)然后编写一个 PHP 脚本在需要时插入行(这是在用户实际搜索时完成的)
  3. Your main page where you want stuff to be displayed simply gets the data back out and puts them into a link container $nameOfWebsite您想要显示内容的主页只需将数据取出并将它们放入链接容器 $nameOfWebsite
  4. It's probably best to use a for/while loop to do step 3最好使用 for/while 循环来执行第 3 步
  5. You could additionally add sth like a counter to know what searches are the most popular / this would be another field in MySQL and you just keep updating it (increasing it by one, but limited to the IP)您还可以添加诸如计数器之类的内容以了解最受欢迎的搜索/这将是 MySQL 中的另一个字段,您只需不断更新它(将其增加一个,但仅限于 IP)

Ok, your question is not yet clear.好的,你的问题还不清楚。 But I'm guessing that you mean you want to READ the latest results first.但我猜你的意思是你想先阅读最新的结果。

To achieve this, follow these steps:为此,请按照下列步骤操作:

  1. When storing the results use an extra field to hold DATETIME.存储结果时,使用一个额外的字段来保存 DATETIME。 So your insert query will look like this:所以您的插入查询将如下所示:

    Insert into Table (SearchItem, When) Values ($strSearchItem, Now() )插入表 (SearchItem, When) 值 ($strSearchItem, Now() )

  2. When retrieving, make sure you include an order by like this:检索时,请确保您包含这样的订单:

    Select * from Table Order by When Desc Select * 来自表 Order by When Desc

I hope this is what you meant to do:)我希望这是你的意思:)

Create a database, create a table (for example recent_searches ) and fields such as query (the query searched) and timestamp (unix timestamp that the query was made) said, then for your script your MySQL query will be something like:创建一个数据库,创建一个表(例如recent_searches )和query (搜索的查询)和timestamp (进行查询的unix时间戳)等字段,然后对于您的脚本,您的 MySQL 查询将类似于:

SELECT * FROM `recent_searches` ORDER BY `timestamp` DESC LIMIT 0, 5

This should return the 5 most recent searches, with the most recent one appearing first.这应该返回最近的 5 次搜索,最近的一次出现在最前面。

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

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