需要多个参数的高级搜索

时间:2014-08-03 22:00:38

标签: php mysql

我正在开发高级搜索功能,但无法让它返回任何值。

mysql_select_db($database_Audiologiska, $Audiologiska);
$where = array();
if (isset($_POST['Personnummer_search'])) 
{   
    $searchword = $_POST['Personnummer_search'];
    $where[] =  "vanster_implantat.patv LIKE '%".$searchword."%'";
}
if(isset($_POST['Namn_search']))
{
    $name = $_POST['Namn_search'];
    $where[] =  "person.Namn LIKE '%".$name."%'";

}
if(isset($_POST['Efternamn_search']))
{
    $surname = $_POST['Efternamn_search'];
    $where[] =  "person.Efternamn LIKE '%".$surname."%'";

}
if(count($where))
{
   $query_SokvansterImp = "SELECT * FROM patient left join person on person.Personnummer = patient.Patient
    left join vanster_implantat on vanster_implantat.Patv = patient.Patient
     WHERE ".implode(" AND ",$where);
}
else//Visa all data
{

    $query_SokvansterImp = "select * from patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.patv = patient.Patient";

}
$SokvansterImp = mysql_query($query_SokvansterImp, $Audiologiska) or die(mysql_error());
$row_SokvansterImp = mysql_fetch_assoc($SokvansterImp);
$totalRows_SokvansterImp = mysql_num_rows($SokvansterImp);

你能看出这段代码是否有什么问题(除了它不是mysqli而不是mysql)?

1 个答案:

答案 0 :(得分:0)

您是否尝试过手动运行该查询;例如使用mysql命令行工具还是phpMyAdmin?

如果你这样做,那么你应该得到任何潜在错误,错别字等的反馈......

另外 - 为了清楚起见,您是否真的对查询字符串执行mysql_query()mysql_fetch_array/fetch_row/fetch_assoc()? 应该看起来像这样:

    $result = mysql_query($query_SokvansterImp) or die(mysql_error());
    // die(mysql_error()); will cause the script to stop executing on error, 
    //and display the error message - you can leave it out but it's useful for debugging...

    while ($row = mysql_fetch_assoc($result)) {
        // Do stuff with the returned data - can be referenced by result column name
    }

在评论#3之后编辑:

尝试使用isset()在if语句的条件中交换if ($value != '')
可能是设置了值(导致isset()返回true),即使那里没有真正的文本......