0

I am working on an ajax search form.

With my current SQL query, it only returns the result when I have the keyword value from the search form EXACTLY the same as the value of a record in the database.

 $sql = "SELECT * FROM events WHERE name LIKE '%" . $_POST["search"] . "'";

Example:

"New York Marathon"

The result will not show until I've typed out "New York Marathon" fully.

How can I modify the query to show the result when I start typing "N..". I would like to apply this for all search terms

cpcdev
  • 1,130
  • 3
  • 18
  • 45
  • 3
    You have to end it with a % I think as well. The first one will only check similarities at the beginning. – Jay Mason Dec 17 '18 at 19:40
  • 2
    Also you should never select * - even if you want all of the columns you should list them out. – Jay Mason Dec 17 '18 at 19:41
  • "when I start typing "N.."....are you asking for an _autocomplete_ i.e. it starts to show you suggestions in a dropdown as you type, like a search engine? For that your current query (possibly with an extra % at the end, as mentioned) would do fine, but you need to execute it a lot of times - every time the user types a key in fact – ADyson Dec 17 '18 at 19:46
  • @ADyson No sorry.. Jay Mason answered the question just waiting to mark it. Thanks for your effort to help though – cpcdev Dec 17 '18 at 19:47

0 Answers0