[英]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:在构建最受欢迎的搜索并很好地扩展它方面,我建议:
I like this approach because:我喜欢这种方法,因为:
Create table (something named like latest_searches
) with fields query
, searched_count
, results_count
.创建带有字段
query
、 searched_count
、 results_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 将它们显示在网站上。
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:为此,请按照下列步骤操作:
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() )
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.