2016-04-03 48 views
1

顯示的記錄數我用的是這樣的,從數據庫中刪除記錄:從數據庫

// did user press submit 
if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
// what button did they press 
if (isset($_POST['delete'], $_POST['id'])) { 
    // we have been request to delete 
    // and we have an id to control the delete 

    // instantiate the class we need 
    $cat = new Category($conn); 

    // clean up our input before we us it in a SQL query 
    $id = filter_input (INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT); 

    // call the method we want to run 
    $cat->deleteSelected($id); 
} 
} 
?> 


<form action="" method="post"> 
<label>Category id :</label> 
<input type="text" name="id" id="id" required="required" placeholder="Please  Enter Id"/><br /><br /> 
<input type="submit" value="Delete" name="delete"/><br /> 
</form> 

現在,我想要做的是從一個數據庫中使用該功能顯示的記錄數:

// countAll(): This function will count all the categories and return it. 
public function countAll() { 
    $query = 'SELECT * FROM categories'; 

    $stmt = $this->conn->prepare($query); 
    $stmt->execute(); 
    $count = $stmt->rowCount(); 

    return $count; 
} 

我必須在第一個代碼(或第二個代碼)中進行更改,以便在單擊按鈕後在類別表中顯示記錄數量?

回答

2

只需回聲countAll()的結果

<?php 
    echo countAll(); 

    public function countAll() { 
    $query = 'SELECT * FROM categories'; 

    $stmt = $this->conn->prepare($query); 
    $stmt->execute(); 
    $count = $stmt->rowCount(); 

    return $count; 
} 
?>