2016-01-06 58 views
0

我從表中填充表中的行,我有一個回聲<td>裏面的表,我一直在努力完成是在mysql_fetch_array內,回聲「Absent」如果查詢沒有返回該特定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>";" 
+0

能否請您提供用於查詢的SQL?您的select語句是否真的是「login_datetime」列? –

回答

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>"; 
} 

?>