$sql = sprintf( "SELECT topic_title
FROM `phpbb_topics`
WHERE `topic_title` LIKE '%%%s%%' LIMIT 20"
, mysql_real_escape_string('match this title')
);我在phpMyAdmin中运行这个查询,结果是:(正确)
match this title
match this title 002但是当我在PHP中运行相同的MYSQL查询时,我得到:(不正确)
match this title 002我也尝试过用php和phpMyAdmin进行匹配,结果是一样的:
$sql = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('match this title' IN BOOLEAN MODE)";我用来搜索的整个代码块如下:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("phpbb") or die(mysql_error());
$query = "match this title";
$query = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('$query' IN BOOLEAN MODE)";
// Doesn't work (these 2 both give the same result "match this title 002" and no the "match this title")
// $query = "SELECT * FROM `phpbb_topics`
// WHERE `topic_title`
// LIKE '%$query%'
// LIMIT 0, 30 "; // Doesn't work
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$topic_title = $row['topic_title'];
echo "$topic_title";
}你知道我做错了什么吗?
我到处找,几乎找不到任何帮助:
发布于 2010-04-06 21:34:35
问题是,在执行查询之后,获取第一行,不对其执行任何操作,通过获取第二行进入循环,并开始打印结果。
如果删除第一个$row = mysql_fetch_array($result) (紧跟在$result = mysql_query($query) or die(mysql_error());之后),应该没问题。
另一个注释;如果你回显一个变量,你不需要在它周围加上任何qoutes。按照现在的方式,您不会在结果之间换行,所以您可能希望将该行更改为echo $topic_title . "<br>";
https://stackoverflow.com/questions/2582705
复制相似问题