显示数据库中的记录数
Displaying number of records from database
我使用类似这样的方法从数据库中删除记录:
// 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;
}
我必须在第一个代码(或第二个代码)中更改什么才能使其在单击按钮后显示类别 table 中的记录数量?
简单地回显 countAll() 的结果
<?php
echo countAll();
public function countAll() {
$query = 'SELECT * FROM categories';
$stmt = $this->conn->prepare($query);
$stmt->execute();
$count = $stmt->rowCount();
return $count;
}
?>
我使用类似这样的方法从数据库中删除记录:
// 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;
}
我必须在第一个代码(或第二个代码)中更改什么才能使其在单击按钮后显示类别 table 中的记录数量?
简单地回显 countAll() 的结果
<?php
echo countAll();
public function countAll() {
$query = 'SELECT * FROM categories';
$stmt = $this->conn->prepare($query);
$stmt->execute();
$count = $stmt->rowCount();
return $count;
}
?>