2015-06-30 14 views
1

我希望從數據庫中找到某些項目的計數。我用這個代碼找到數據庫中的數據計數

$sql=" SELECT count(*) from request WHERE status = '0'"; 
$result = mysqli_query($con, $sql); 
if(mysqli_num_rows($result)>0) 
    { 
     while($row = mysqli_fetch_assoc($result)) 
      { 
       echo "<pre>"; 
        print_r($row); 
       echo "</pre>"; 

      } 
    } 

我得到這個數組中

Array 
(
    [count(1)] => 1 
) 

行從該數組提取值,我用

$total = $row[0]; 
echo $total; 

,但沒有得到任何結果。我如何從這個數組中取值

回答

0

您需要使用:

​​

或更改您的查詢:

$sql=" SELECT count(*) as `num` from request WHERE status = '0'"; 

及用途:

$row = mysqli_fetch_assoc($result)); 
echo $row['num']; 
0

我認爲如果條件在你的代碼中你不需要。你可以這樣做

$sql=" SELECT * from request WHERE status = '0'"; 
$result = mysqli_query($con, $sql); 

     while($row = mysqli_fetch_assoc($result)) 
      { 
       echo "<pre>"; 
        print_r($row); 
       echo "</pre>"; 

      }