2013-01-31 47 views
0

我有一個用OR編譯的搜索查詢,但是我想用AND來得到更少的結果。不幸的是,由於一些參數(VatStatut,VarDomaine等)在輸入表單中留空,我根本得不到結果。我如何過濾掉這些空參數?使用AND運算符的Mysql搜索查詢:篩選出空參數

$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", 

回答

0

嘗試使用empty()而不是isset。這樣,如果變量存在但是空字符串參數不會被設置。

I.e.

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

(請注意,我們使用的是!empty()