1

I have a input field that search's for keywords in my database. Say the title of a car listing is "Toyota surf 1990 SSR-X", I want to be able to search something like "Toyota 1990", how can I achieve this?. If I search "Toyota surf" or "surf 1990" it works.

$query = "SELECT * FROM cars WHERE title LIKE '%" . $search_keywords . "%'";
3
  • 1
    Look into fulltext search Commented Sep 13, 2015 at 20:47
  • well if year,make,model, etc are columns in that table(which would make sense for a table called cars?), you can build your where clause dynamically like: WHERE make = 'Toyota' AND model = 'Surf' or WHERE model = 'surf' AND year = '1990' Commented Sep 14, 2015 at 0:45
  • Notice: Your code is vulnerable to SQL injections. Always sanitize your database inputs Commented Sep 14, 2015 at 11:06

1 Answer 1

3

$query = "SELECT * FROM cars WHERE title LIKE '%" . $search_keywords . "%'";

That code used in MySQL, so it doesn't have any relates with the main PHP coding.

Try using the preg_match feature instead. Here is an example:

preg_match("KEYWORDS", $variable)

And you could add an if statement. Like the following:

if(preg_match("KEYWORDS", $variable))

I wish I could give you the solution that you are requesting for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.