显示数据库中的记录数

时间:2016-04-03 17:48:31

标签: php mysql database-connection

我使用类似的东西从数据库中删除记录:

// 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;
}

在第一个代码(或第二个代码)中我需要更改什么才能在单击按钮后显示类别表中的记录数量?

1 个答案:

答案 0 :(得分:2)

只需回显countAll()

的结果
<?php
  echo countAll();

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

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

    return $count;
 }
?>