(PHP)如果,Else没有正确检索条件中的数据

时间:2016-05-04 03:14:34

标签: php mysql if-statement

我在这里遇到了If-Else条件的问题。如下面的代码,

if($status == 'New'){
    $resultx0 = mysql_query("SELECT * FROM application_data_file WHERE Position_ID = '".$pos_id."'  && Application_Status = 'New'  OR 'Received'  OR 'Checking' ");
}
elseif ($status == 'Not qualified'){
    $resultx0 = mysql_query("SELECT * FROM application_data_file WHERE Position_ID = '".$pos_id."'  && Application_Status = 'Failed'  OR 'Not qualified' ");
}
elseif ($status == 'No Condition'){
    $resultx0 = mysql_query("SELECT * FROM application_data_file WHERE Position_ID = '".$pos_id."' ");  
}

“新建”和“不合格”条件仅适用于“application_data_file”表中的第一个条件。 “新”条件将仅检索“新”但没有“收到”或“检查”,因为我在条件中输入,“不合格”条件将仅检索“失败”而没有“不合格”。

我对此一无所知。条件有什么不对吗?

干杯。

4 个答案:

答案 0 :(得分:3)

你的mysql语法不正确

proc sgplot data = insurance; histogram YOJ; run; proc sgplot data = insurance; histogram INCOME; run; proc sgplot data = insurance; histogram HOME_VAL; run; proc sgplot data = insurance; histogram CAR_AGE; run;

这应该是

Application_Status = 'New' OR 'Received' OR 'Checking'

Application_Status = 'New' OR Application_Status = 'Received' OR Application_Status = 'Checking'

答案 1 :(得分:3)

试试这个

mysql_query("SELECT * FROM application_data_file 
             WHERE Position_ID = '".$pos_id."' 
             AND Application_Status IN ('New','Received','Checking')");

答案 2 :(得分:1)

SQL语句未正确形成;在您的情况下,我建议使用IN来检查Application_Status字段(如下所示);

$pos_id = mysql_real_escape_string($pos_id);

if($status == 'New') {
    $query = "SELECT * FROM application_data_file"
            . " WHERE Position_ID='" . $pos_id . "'"
            . " AND Application_Status IN ('New', 'Received', 'Checking')";

} elseif ($status == 'Not qualified') {
    $query = "SELECT * FROM application_data_file"
            . " WHERE Position_ID='" . $pos_id . "'"
            . " AND Application_Status IN ('Failed', 'Not qualified')";

} else {
    // Includes the case where $status == 'No Condition'
    $query = "SELECT * FROM application_data_file"
            . " WHERE Position_ID='" . $pos_id . "'";

}

$resultx0 = mysql_query($query);

答案 3 :(得分:1)

请仔细阅读代码。

我们只需要检查位置ID的两个条件:

NewNot qualified。对于No Condition,没有检查。

因此,只添加两个条件:NewNot qualified

获取一系列位置ID,如果条件添加IN子句,否则不执行任何操作。

// Initiate a blank array
$positionIds = array();
$status = 'New';
if ($status == 'New') {
    $positionIds[] = 'New';
    $positionIds[] = 'Received';
    $positionIds[] = 'Checking';
}
else if ($status == 'Not qualified') {
    $positionIds[] = 'Failed';
    $positionIds[] = 'Not qualified';
}
$sql = "SELECT * FROM application_data_file WHERE Position_ID = '".$pos_id."";
if (! empty($positionIds)) {
    $comma_separated = implode("','", $positionIds);
    $comma_separated = "'".$comma_separated."'";
    $sql .= " AND Application_Status IN ( " . $comma_separated .  ")";
}
$resultx0 = mysql_query($sql);
  

请不要使用mysql_ *函数,因为它们已被弃用和删除   由于安全原因,完全在PHP 7中。