开发者

Display latest search results from MySQL with PHP

开发者 https://www.devze.com 2023-03-03 10:47 出处:网络
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.

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.

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 l开发者_JAVA百科atest searches in a MySQL database (or anything that'll work), and display them on a page afterwards.

I'm guessing this would best be accomplished with MySQL, and then I'd like to output it in to 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. 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.
  • 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
  • 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 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:

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.


Create table (something named like latest_searches) with fields query, searched_count, results_count.
Then after each search (if results_count>0), check, if this search query exists in that table. And update or insert new line into table.
And on some page you can just use data from this table.

It's pretty simple.


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. So your insert query will look like this:

    Insert into Table (SearchItem, When) Values ($strSearchItem, Now() )

  2. When retrieving, make sure you include an order by like this:

    Select * from Table Order by When Desc

I hope this is what you meant to do :)


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. Then you pull them out of the DB ordered by the timestamp and display them on the website with 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)
  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
  4. It's probably best to use a for/while loop to do step 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号