Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display latest search results from MySQL with PHP

Tags:

php

mysql

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 latest 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.

like image 209
Dustin Avatar asked Dec 02 '25 03:12

Dustin


1 Answers

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
like image 100
James C Avatar answered Dec 03 '25 18:12

James C