如果$ result不存在,则在mysql_fetch_array内部回显值

时间:2016-01-06 19:35:21

标签: php while-loop

我正在从Mysql中填充表格中的行,我在表格中有一个echo <td>,我试图完成的是在mysql_fetch_array中,以回显&#34;不存在&#34;如果查询没有返回该特定html表行的任何记录,我尝试使用mysql_num_rows == 0,但它不起作用,因为查询中存在行,因此使用此方法不行。

$sqlsun = "SELECT * FROM cscc_attendance WHERE cscc_attendance.empId= '$row[empId]' AND cscc_attendance.login_datetime>'".$this_start1->format('Y-m-d 00:00:00')."' AND cscc_attendance.login_datetime<'".$this_start1->format('Y-m-d 23:59:59')."' GROUP BY cscc_attendance.empId";

    $sqlsunexe = mysql_query($sqlsun);

     echo "<td style=\"background-color:#e6e6e6\"><b>";

      while($rowdate1 = mysql_fetch_array($sqlsunexe))
    {   
     if (empty($rowdate1["login_datetime"])) 
        {
            //IF NO MYSQL RESULT, ECHO TO ROW TD:
            echo "ABSENT";
        }
        else
        {
            //THERE IS A QUERY RESULT, FILL THE TD IN TABLE:
            $newhour = new DateTime($rowdate1["ready_time"]);
            $newhourmodstr = $newhour->modify('-1 hour');
            echo $newhourmodstr->format('H:i:s');
        }
    }
   echo "</b></td>";"

1 个答案:

答案 0 :(得分:1)

这是你的代码应该如何使用mysqli:

<?php
$con = mysqli_connect("localhost", "root", "", "database");
// Evaluate the connection
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
} else {
    $q = "SELECT * FROM `tablename` WHERE `fieldname` = 'value'";
    $sqlsunexe = mysqli_query($con, $q);

    echo "<td style=\"background-color:#e6e6e6\"><b>";
    // check if return data
    if ($sqlsunexe->num_rows > 0){
        while($rowdate1 = mysqli_fetch_array($sqlsunexe)){
            //THERE IS A QUERY RESULT, FILL THE TD IN TABLE:
            $newhour = new DateTime($rowdate1["ready_time"]);
            $newhourmodstr = $newhour->modify('-1 hour');
            echo $newhourmodstr->format('H:i:s');
        }
    } else {
        //IF NO MYSQL RESULT, ECHO TO ROW TD:
        echo "ABSENT";
    }

   echo "</b></td>";
}

?>