php搜索脚本返回相同的结果

时间:2015-08-28 12:03:54

标签: php

我的搜索脚本存在一些输出问题 - 无论搜索字词如何,我总是得到相同的输出结果。我的回音查询似乎没问题,但我总是得到与输出相同的结果。这是我的剧本:

if (isset($_GET['search'])) {
if($_GET['search'] == "search"){
$loop = "";
$search = !empty($_GET['find']) && ($_GET['find'] != "")?strip_tags($_GET['find']):false;
$search = trim($search);
$field = !empty($_GET['field'])?strip_tags($_GET['field']):false;
$field = trim($field);

if ($search == ""){
    echo "Please enter search conditions !";
}
elseif ($search != ""){
echo "<u>Searched term</u>:"." ".$search."<br />"."<u>In</u>:"." ".$field."<br/><br/>";
$sql = "SELECT p_url FROM papers";
$query = mysql_query($sql);
$condition = array();

if ($field == "author"){
$condition[] = "WHERE p_authors LIKE '%$search%'";
}   
if ($field == "papername"){
$condition[] = "WHERE p_name LIKE '%$search%'";
}
if ($field == "coauthors") {
$condition[] = "WHERE p_cauthor LIKE '%$search%'";
}
if ($field == "abstract"){
$condition[] = "WHERE p_abstract LIKE '%$search%'";
}
if ($field == "keywords"){
$condition[] = "WHERE p_abstract LIKE '%$search%'";
}

$sql .= " ".implode(" ", $condition);
echo "$sql"."<br />";
$out = mysql_num_rows($query);
$result = "";

if ($out){
   while ($result = mysql_fetch_assoc($query)){
      foreach ($result as $val){
         echo "$val"."<br />";
      }
  }
}
}

有什么建议吗? THX

1 个答案:

答案 0 :(得分:1)

在将条件添加到$ sql变量后,需要设置$ query变量。现在你只需执行无任何条件的查询。