删除数据表中的记录使用delete语句,改语句返回删除的记录数量
~~~
$result=Db::name('book')->where(['id'=>$id])->delete();
~~~
~~~
<?php
namespace app\controller;
use app\BaseController;
use think\facade\Db;
class Book extends BaseController
{
public function getBookList($page = 1, $pageSize = 10)
{
// 获取数据集
$books = Db::name('book')->order('id', 'desc')->paginate(['page' => $page, 'list_rows' => $pageSize]);
$books = $books->toArray();
//转换分页器满足约定的接口规范
$data = array(
'count' => $books['total'],
'currentPage' => $books['current_page'],
"pageSize" => $books['per_page'],
"totalPages" => $books['last_page'],
"data"=>$books['data']
);
$response = [
'errno' => 0,
'errmsg' => '',
'data' => $data
];
return json($response);
}
public function getAllBook()
{
// 获取数据集
$books = Db::name('book')->select();
$response = [
'errno' => 0,
'errmsg' => '',
'data' => $books
];
return json($response);
}
public function getBookInfo($id = 0)
{
$book = Db::name('book')->where(['id' => $id])->find();
if ($book) {
$response = [
'errno' => 0,
'errmsg' => '',
'data' => $book
];
} else {
$response = [
'errno' => 1000,
'errmsg' => '没有满足条件的数据',
'data' => null
];
}
return json($response);
}
public function deleteBookInfo($id){
$result=Db::name('book')->where(['id'=>$id])->delete();
$response= null;
$response=[
'errno' => 0,
'errmsg' => '删除数据的条数',
'data' => $result
];
return json($response);
}
}
~~~