使用AND运算符的Mysql搜索查询:过滤掉空参数

时间:2013-01-31 21:39:49

标签: php mysql

我有一个使用OR的搜索查询构建,但我想使用相同的AND来获得更少的结果。不幸的是,由于一些参数(VatStatut,VarDomaine等)在输入表格中保留为空,我根本得不到任何结果。如何过滤掉这些空参数?

$VarStatut_jeu_res1 = "%";
if (isset($_POST['choix_statut'])) {
$VarStatut_jeu_res1 = $_POST['choix_statut'];
}

$VarSujet_jeu_res1 = "%";
if (isset($_POST['choix_sujet'])) {
$VarSujet_jeu_res1 = $_POST['choix_sujet'];
}

$VarDateEcheance1_jeu_res1 = "%";
if (isset($_POST['choix_dateecheance1'])) {
$VarDateEcheance1_jeu_res1 = $_POST['choix_dateecheance1'];
}

$VarDateEcheance2_jeu_res1 = "%";
if (isset($_POST['choix_dateecheance2'])) {
$VarDateEcheance2_jeu_res1 = $_POST['choix_dateecheance2'];
}

$VarDateOrigine1_jeu_res1 = "%";
if (isset($_POST['choix_dateorigine1'])) {
$VarDateOrigine1_jeu_res1 = $_POST['choix_dateorigine1'];
}

$VarDateOrigine2_jeu_res1 = "%";
if (isset($_POST['choix_dateorigine2'])) {
$VarDateOrigine2_jeu_res1 = $_POST['choix_dateorigine2'];
}

$VarTheme_jeu_res1 = "%";
if (isset($_POST['choix_theme'])) {
$VarTheme_jeu_res1 = $_POST['choix_theme'];
}

$VarAuteur_jeu_res1 = "%";
if (isset($_POST['choix_auteur'])) {
$VarAuteur_jeu_res1 = $_POST['choix_auteur'];
}

$VarDomaine_jeu_res1 = "%";
if (isset($_POST['choix_domaine'])) {
$VarDomaine_jeu_res1 = $_POST['choix_domaine'];
}

mysql_select_db($database_ConnJournal, $ConnJournal);

$query_jeu_res1 = sprintf("SELECT * FROM Projets1 WHERE Projets1.Statut LIKE %s OR       Projets1.Domaine LIKE %s OR Projets1.Auteur LIKE %s OR Projets1.Theme LIKE %s OR Projets1.DateOrigine BETWEEN %s AND %s OR Projets1.DateEcheance BETWEEN %s AND %s OR MATCH (Sujet) AGAINST (%s) ORDER BY Projets1.DateHeure DESC", 

1 个答案:

答案 0 :(得分:0)

尝试使用empty()代替isset。这样,如果变量存在但是空字符串,则不设置参数。

$VarDomaine_jeu_res1 = "%";
if (!empty($_POST['choix_domaine'])) {
    $VarDomaine_jeu_res1 = $_POST['choix_domaine'];
}

(注意我们正在使用!empty()