Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am attempting to build a small search engine, that can browse articles on my site. I have so far written the PHP code, which my server has no issue with. What the server does take issue with is my SQL code, which as far as I am aware is correct but it says it is 'not the right syntax.'

I am trying to add a PHP variable to the statement, to get it to search the contents of that variable as the contents has the form input in it. The variable (as you will see) is called $search_input My code is here:

$sql_code = "SELECT * FROM 'articles' WHERE 'tags' LIKE ‘%{$search_input}%’";

I am sure I just have an issue with my syntax, but I can't see where. I have looked back over W3schools guides to WHERE clauses and LIKE operators - cannot find anything. I would gratefully appreciate any help - thanks.

  • Thanks for all the help guys - I tried Des' first post and it worked with the different posts. But if I want to write: 'The Following Results were found' in the PHP. It then repeats it after each search result from the database.
  • And it displays this in the browser:

    The Following Results Were Found:

    HelloWorldPrimaryTest Take a look This document has the following tags: hello world primary

    The Following Results Were Found:

    Take a look This document has the following tags: home

    I don't want it to repeat the 'The Following Results Were Found: ' Can anyone help? Thanks very much for all your help so far.

    Wrong quotes, try this:

    $sql_code = "SELECT * FROM articles WHERE tags LIKE '%{$search_input}%'";
    

    It would be better to use prepared statements (PDO).

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.