2016-04-25 77 views
0
// Populate headers 
    $fields = $result->fetch_fields(); 
    foreach ($fields as $field) 
     printf("<th>%s</th>", $field->name); 

    printf("</tr>"); 

    // Write to table 
    while ($myvar = $result->fetch_row()) { 

     $date = $myvar[0]; 
     $room_ID = $myvar[1]; 
     $description = $myvar[2]; 
     $firstname = $myvar[3]; 
     $lastname = $myvar[4]; 
     $message = $myvar[5]; 
     $period = $myvar[6]; 

     printf("<tr>"); 
     printf("<td>%s</td><td>%s</td>", $date, $room_ID); 
     printf("<td>%s</td><td>%s</td>", $description, $firstname); 
     printf("<td>%s</td><td>%s</td><td>%s</td>", $lastname, $message, $period); 
     printf("</tr>"); 

    } 

我試圖回顯「沒有結果顯示」,如果我的MySQLi結果返回空。問題是標題是作爲數組的一部分返回的,我該如何實現?如何檢查MySQL結果是否爲空

+3

'num_rows()'會有幫助。 http://php.net/manual/en/mysqli-result.num-rows.php –

回答

0

您可以使用num_rows從查詢中獲取記錄回溯的總值。

$row_cnt = $result->num_rows; 
if($row_cnt>0){ echo "Have data";}else{echo "No data";} 
0

您需要檢查行數。如果它返回零意味着表不包含行。嘗試下面的代碼。

// Populate headers 
    $fields = $result->fetch_fields(); 
    foreach ($fields as $field) 
     printf("<th>%s</th>", $field->name); 

    printf("</tr>"); 

    // Write to table 
    if($result->num_rows== 0){ 
    echo "No Result to display"; 
    }else{ 
    while ($myvar = $result->fetch_row()) { 

     $date = $myvar[0]; 
     $room_ID = $myvar[1]; 
     $description = $myvar[2]; 
     $firstname = $myvar[3]; 
     $lastname = $myvar[4]; 
     $message = $myvar[5]; 
     $period = $myvar[6]; 

     printf("<tr>"); 
     printf("<td>%s</td><td>%s</td>", $date, $room_ID); 
     printf("<td>%s</td><td>%s</td>", $description, $firstname); 
     printf("<td>%s</td><td>%s</td><td>%s</td>", $lastname, $message, $period); 
     printf("</tr>"); 

    } 

}

0

簡單使用NUM_ROWS

if($result->num_rows== 0){ 
echo "no results to display" 
    } 
else{ 
    while ($myvar = $result->fetch_row()) { 
    //Your Code here 
} 
0

您可以使用num_rows得到記錄的總價值。

$row_count = $result->num_rows; 
if($row_count>0) 
{ 
    echo "Data Available"; 
} 
else 
{ 
    echo "No Data Available"; 
} 
相關問題