2013-08-21 50 views
0

我们有像准备LIKE语句

SELECT title, link 
    FROM search 
    WHERE title LIKE '%$keyword%' 

声明我怎样才能使一个事先准备好的声明,使用户输入正确消毒,如:

​​

上面似乎失败,我甚至尝试过'%'+?+'%''%'.?.'%'

回答

2

使用CONCATMysql Docs function in your SQL

SELECT title, link 
    FROM search 
    WHERE title LIKE CONCAT('%', ?, '%') 
        ################### 

它接受参数要绑定的方式。因此,准备好的声明。

了解更多在重复的问题的答案的详细描述:

+0

这是否被认为是一种良好做法?我看到了优势,你不必编写应用程序逻辑来支持“like”语句。但我不确定这是否应该在应用程序中使用。 –

+0

您也可以用一个问号替换整个虚线部分,并从您的应用程序中提供字符串表达式。它只是显示你需要一个参数作为参数 - 而不是在一个字符串表达式中,例如寻找问号。您可以通过指向重复问题的链接找到更多信息。 – hakre

-1

在Perl中,你必须在%-placeholder添加到变量至极,你会使用,而不是:

select searchtitle,searchdescription,searchlink from search where 
searchtitle like ? or searchdescription like ? 

使用下面的绑定PARAMS为SQL语句:

Bindparams = ['%searchtitle%', '%searchdesc%']; 

我认为这也必须在PHP中完成。

1

它只是如下所示?

$stmt = $dbh->prepare("select searchtitle,searchdescription,searchlink from search where 
searchtitle like :title or searchdescription like :description"); 
$stmt->bindParam(':title', '%' . $title . '%'); 
$stmt->bindParam(':description', '%' . $description . '%'); 
$stmt->execute(); 

$stmt->bindParam(':title', "%$title%"); 
$stmt->bindParam(':description', "%$description%"); 

我已经修改了示例given here

+0

这不会产生相同的结果。 – user2703510