I created a search engine on my website where user can input whatever they like and show the result. Below is the code I use to get the result but it is not working because of the syntax error.
In my database I have 10 rows, each rows contain 4 columns(id, author, second_author, book_name). My goal is, if the user enter a name that is found in 'author', I want to retrieve that result, 'OR' if they enter a name that is found in book_name I want to retrieve that result and so on for the other columns..
I know the proper way to do this is by using LIKE Operator, but how about if you want to compare the keywords to several columns, what should you do?
I've tried this but was not working:
SELECT * FROM book_list WHERE author OR second_author OR book_name LIKE '%".$search_key."%'
It's just a syntax tweak:
SELECT * FROM book_list
WHERE author LIKE '%".$search_key."%'
OR second_author LIKE '%".$search_key."%'
OR book_name LIKE '%".$search_key."%'
Hope that helps
Your query has an error, you have to search on each column as
SELECT * FROM book_list WHERE author LIKE '%".$search_key."%' OR second_author LIKE '%".$search_key."%' OR book_name LIKE '%".$search_key."%'
I would also like you to be aware that you are at risk of sql injection, have a look here How can I prevent SQL injection in PHP?. You should use prepared statment to avoid any risk