2017-09-16 16 views
0

我怎樣才能dispaly「無項目」時,數據庫沒有values.here是我的代碼,我應該在哪裏需要把else{ echo 'No item';}如何識別mysqli_stmt_fetch返回0

if ($stmt = mysqli_prepare($link, "SELECT id,name,price,image FROM product Limit ?,8")) { 

    mysqli_stmt_bind_param($stmt, "s", $page_1); 
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_bind_result($stmt,$ids,$title,$prc,$image); 

     while (mysqli_stmt_fetch($stmt)) { 

     $id = $ids; 
     $pst_title = $title; 
     $price = $prc; 
     $pst_image = $image; 
     $price = number_format($price, 2); 

     echo ' <article>'; 
     echo " <img src='admin/gem/$pst_image' alt='images'></td>"; 
     echo " <h3>$pst_title</h3>"; 
     echo " <h4>$.$price</h4>"; 
     echo " <a href='gem.php?id={$id}' class='btn-add'>View more</a>"; 
     echo ' </article>';          
     } 

} 
+0

可以設置一個計數器在while語句還是有PDO中的'num_results'樣式元素? 'if($ stmt-> fetchColumn()> 0)'或類似的東西? – Twisty

回答

1

您可以檢查返回的行數通過查詢,訪問num_rows屬性。但是,要保持正確的值,您需要首先使用mysqli_stmt_store_result()來存儲結果。請參閱下面的修改後的代碼以及添加的評論。

if ($stmt = mysqli_prepare($link, "SELECT id,name,price,image FROM product Limit ?,8")) { 

    mysqli_stmt_bind_param($stmt, "s", $page_1); 
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_bind_result($stmt,$ids,$title,$prc,$image); 
    mysqli_stmt_store_result($stmt); // Store the result into memory 
    if (mysqli_stmt_num_rows($stmt)) { // Check the number of rows returned 
     while (mysqli_stmt_fetch($stmt)) { 

      $id = $ids; 
      $pst_title = $title; 
      $price = $prc; 
      $pst_image = $image; 
      $price = number_format($price, 2); 

      echo ' <article>'; 
      echo " <img src='admin/gem/$pst_image' alt='images'></td>"; 
      echo " <h3>$pst_title</h3>"; 
      echo " <h4>$.$price</h4>"; 
      echo " <a href='gem.php?id={$id}' class='btn-add'>View more</a>"; 
      echo ' </article>';          
     } 
    } else { 
     echo 'No item'; 
    } 
} 
+0

非常感謝,很好的解釋:) +1 – Rooter

1

這將是做事情更加美好的方式:http://php.net/manual/ro/mysqli-result.fetch-array.php

if ($stmt = mysqli_prepare($link, "SELECT id,name,price,image FROM product Limit ?,8")) { 

    mysqli_stmt_bind_param($stmt, "s", $page_1); 
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_store_result($stmt);//thanks to Qirel 
    mysqli_stmt_bind_result($stmt,$ids,$title,$prc,$image); 
    if(mysqli_stmt_num_rows($stmt)>0){ 
     while (mysqli_stmt_fetch($stmt)) { 

     $id = $ids; 
     $pst_title = $title; 
     $price = $prc; 
     $pst_image = $image; 
     $price = number_format($price, 2); 

     echo ' <article>'; 
     echo " <img src='admin/gem/$pst_image' alt='images'></td>"; 
     echo " <h3>$pst_title</h3>"; 
     echo " <h4>$.$price</h4>"; 
     echo " <a href='gem.php?id={$id}' class='btn-add'>View more</a>"; 
     echo ' </article>';          
     } 
    }else{ 
      echo 'No item'; 
    } 
} 
+0

從PHP手冊:「* mysqli_stmt_num_rows()的使用取決於您是否使用mysqli_stmt_store_result()來緩衝語句句柄中的整個結果集。如果你使用'mysqli_stmt_store_result()','mysqli_stmt_num_rows()'可能立即被調用。*「 – Qirel