2016-08-16 53 views
0

我正在使用mysql_num_rows($查詢);函數從表中獲取總行數。但它顯示零計數。

這是我的代碼

<form method="POST" action="processor.php"> 
<!-- start data table --> 
    <table id="table1" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
    <thead> 
     <tr><th>Video ID</th><th>Video Name</th></tr> 
    </thead> 
    <?php 
     $access = $_SESSION['login']; 
     if($access == "white"){ 
       $sql = "SELECT * FROM videos ORDER BY id DESC"; 
       $query = $conn->query($sql); 
       // Return the number of rows in result set 
       $rowcount=mysql_num_rows($query); 
       printf("Result set has %d rows.\n",$rowcount); 
         $i=1; 
         while ($row = $query->fetch(PDO::FETCH_ASSOC)){ 
         $id = $row['id']; 
         $video_name = $row['video_name']; 
         echo " 
          <tbody> 
          <tr> 
           <td style='text-align:center;'>$id</td> 
           <td id='test10' class='text-center'><b>$video_name</b></td> 
          </tr> 
          </tbody>"; 
        } $i++; 
       } 
     else { 
      header ("location:index.php"); 
      } 
     ?> 
    </table> 
    <!-- End data table --> 
</form>  

我想從上面的代碼這一說法得到行的張數。

printf("Result set has %d rows.\n",$rowcount); 

我的結果圖像也附上,它只顯示0行。但我的數據顯示在圖像下方。它不是零。請幫助如何計數。在此先感謝

this is the result when i run above code. it shows 0 rows

+1

不再使用'mysql_ *'功能。他們已被棄用多年。改用'mysqli_ *'或PDO。 – Bobby

+0

如何使用PDO來計數。 ? –

+0

感謝兄弟。我使用這個$ rowcount = $ query-> rowCount()得到結果; –

回答

1

您可以使用PDO數據庫模塊來獲取你的數據。我知道這是因爲這條線$query->fetch(PDO::FETCH_ASSOC)。如果您使用pdo模塊,請閱讀Doku如何獲取行計數。

+0

感謝兄弟。我使用這個$ rowcount = $ query-> rowCount()得到結果; –

1

通過使用PDO的這條語句,我得到了總數。

$rowcount = $query->rowCount();