如何使用LIKE运算符修复SQL错误?

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:

Home 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).

Give this a go

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

remove the curly brackets and try

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

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

You shouldn't use quotes for table names or row names, you can leave it empty or use ticks (table_name) instead