如何检查Mysql Time Field是否为NULL

时间:2018-05-03 09:31:00

标签: php mysql sql mysqli

我是编程并尝试制作时间表的新手。我的问题是,无论我尝试什么,我都无法让我的PHP检查我的DB在endWork上的值是否设置为NULL。 (目前为空)

<?php
if (isset($_POST['setTimestamp'])) {
$date = $_POST['date'];
$workDate = date('Y-m-d');


$dateQuery = "SELECT WorkDate FROM WorkingHours WHERE WorkDate = 
'$workDate'";
$eWorkQuery = "SELECT EndWork FROM WorkingHours WHERE WorkDate = 
'$workDate'";
$eBreakQuery = "SELECT EndBreak FROM WorkingHours WHERE WorkDate = 
'$workDate'";
$sBreakQuery = "SELECT StartBreak FROM WorkingHours WHERE WorkDate = 
'$workDate'";
$sWorkQuery = "SELECT StartWork FROM WorkingHours WHERE WorkDate = 
'$workDate'";


$d_query_run = mysqli_query($con, $dateQuery);
$e_w_query_run = mysqli_query($con, $eWorkQuery);
$e_b_query_run = mysqli_query($con, $eBreakQuery);
$s_b_query_run = mysqli_query($con, $sBreakQuery);
$s_w_query_run = mysqli_query($con, $sWorkQuery);


if (mysqli_num_rows($d_query_run)==1) {

    if (!isset($e_w_query_run)) {

        if (empty($e_b_query_run)) {

            if (empty($s_b_query_run)) {

                if (empty($s_w_query_run)) {
                }
                else{
                    echo " set StartBreak's time to localtime ";
                }


            }
            else{
                echo " set EndBreak's time to localtime ";
            }


        }
        else{
            echo " set EndWork's time to localtime ";
        }


    }

    else{
        echo " You already submited your timesheet for today ";
        echo "<pre>" ,print_r($e_w_query_run, TRUE),"</pre>";
    }


}
else{
    echo " set StartWork's time to localtime";
}

}
?>

下面的代码行只是检查我的查询的输出。

echo "<pre>" ,print_r($e_w_query_run, TRUE),"</pre>";

我用!isset() is_null empty()尝试过。 它主要告诉我,它不能在对象上使用这些操作,而是在数组上使用。因此,我尝试使用mysqli_fetch_array,但这也没有用。

如果有人可以帮我解决这个问题会很棒,并且还要提前感谢。

1 个答案:

答案 0 :(得分:2)

仅运行mysqli_query不会为您提供数据。要访问它,您需要执行以下操作:

$qyery = mysqli_query($con, $eWorkQuery);
$result = mysqli_fetch_array($query);
if(is_null($result['EndWork'])){
  echo " set EndBreak's time to localtime "; 
}

或者如果你想使用对象

$result = mysqli_fetch_object($query);
if(is_null($result->EndWork)){
  echo " set EndBreak's time to localtime "; 
}