2012-07-18 65 views
0

我需要在mysql表中搜索某个单词以在特定列(type:text)中查找。
MySQL搜索

我认为最简单的方法就是做:

$str = "something"; # let's say passed by POST or GET from a form 
$sql = "select lala, textThing from table_name where dudu=1 and textThing LIKE %'$str'%"; 

这似乎如果在MySQL做手工查询工作得很好,但不是通过网络PHP。 它抛出

您的SQL语法有错误;检查对应于你的MySQL服务器版本在线路附近使用“%searchedString%”正确的语法手册1

其中searchedString是,嗯,你知道,我搜索了:)

字符串首先PHP看起来是这样的:

$str = NULL; 


echo "<p>Search for a string</p>"; 
echo "<form name='formSearch' action='result.php' method='post'>"; 
echo "<input type='text' name='text' size='30'/>"; 
echo "<input type='submit' name='search' value='Search'/>"; 
echo "</form>"; 

第二个是刚刚起步的“文本”,做MySQL和应该显示数据

回答

2

你必须把引号之间的LIKE子句。在你的php代码中这样逃脱它们:

$sql = "select lala, textThing from table_name where dudu=1 and textThing LIKE \"%".$str."%\"" 
+0

完美的作品!谢谢! – trainoasis 2012-07-18 15:50:40

+0

以及如果我想要显示该词出现在显示的整个行,该怎么办? – trainoasis 2012-07-18 15:53:45

1

你需要在引用或双引号中包括%。

$ sql =“select lala,textThing from table_name where dudu = 1 and textThing LIKE'%$ str%'”;

$ SQL = “选择拉拉,textThing从TABLE_NAME其中嘟嘟= 1和 textThing LIKE '%” + $ STR + “%'”;