简体   繁体   English

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

[英]How do I Fix SQL Error with LIKE Operator?

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. 到目前为止,我已经编写了PHP代码,我的服务器没有问题。 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.' 服务器确实遇到的问题是我的SQL代码,据我所知这是正确的,但它表示“语法不正确”。

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. 我试图在该语句中添加一个PHP变量,以使其能够搜索该变量的内容,因为该内容具有表单输入。 The variable (as you will see) is called $search_input My code is here: 变量(如您所见)称为$ search_input我的代码在这里:

$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. 我回顾了W3schools关于WHERE子句和LIKE运算符的指南-找不到任何东西。 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. 感谢所有帮助人员-我尝试了Des的第一篇文章,它适用于其他文章。 But if I want to write: 'The Following Results were found' in the PHP. 但是,如果我想写: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 HelloWorldPrimaryTest看一下该文档具有以下标签:hello world primary

The Following Results Were Found: 找到以下结果:

Home Take a look This document has the following tags: home Home看一下此文档具有以下标签: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). 最好使用准备好的语句 (PDO)。

快去

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

去除花括号并尝试

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

$sql_code = "SELECT * FROM articles WHERE tags LIKE '%$search_input%'"; $ sql_code =“选择*来自articles tags哪里,例如'%$ search_input%'”;

You shouldn't use quotes for table names or row names, you can leave it empty or use ticks ( table_name ) instead 您不应该在表名或行名中使用引号,而可以将其保留为空或使用刻度( table_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM