MYSQL类似于克隆错误

时间:2015-01-08 04:15:12

标签: php mysql

我有一个表格,我通过它提交输入。

<form action= "index.php" method="POST">
            <input type="text" name="input" required />
            <input type="submit" name="submit" value="ask"/>
        </form>

在php中

$input=$_POST['input'];

假设我提交$input='IS BILL %'我收到错误

  

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以便使用接近&#39; IS BILL%limit 1&#39;

当我在查询中的表格列中搜索输入时..

$getdata= mysql_query("SELECT * FROM aiml WHERE pattern like $input limit 1 ") or die(mysql_error());
if(!$getdata)
        echo "no data";
else
{
        while ($row = mysql_fetch_assoc($getdata)) 
        {
                $text=$row['template'];
                echo $text;
         }
  }

但如果在查询中我写  "SELECT * FROM aiml WHERE pattern like 'IS BILL %' limit 1 ") or die(mysql_error());

我得到了预期的输出。

2 个答案:

答案 0 :(得分:0)

"SELECT * FROM aiml WHERE pattern like '%$input%' limit 1" // mathches the pattern containing $input

"SELECT * FROM aiml WHERE pattern like '$input%' limit 1" // mathches the pattern starting with $input

"SELECT * FROM aiml WHERE pattern like '%$input' limit 1" // mathches the pattern ending with $input

答案 1 :(得分:0)

在输入文本框中,只需传递查询字符串即表示您要搜索的单词IS BILL而不是IS BILL %,在SQL查询中使用%

$input = $_POST['input'];
mysql_query("SELECT * FROM aiml WHERE pattern like '%".$input."%' limit 1 ") 
相关问题