编写数据处理类:list.php
<?php
//首页列表API
//http://127.0.0.1/api/list.php?page=1&pagesize=12
require_once('./response.php');
require_once('./Db.php');
$page = isset($_GET['page'])? $_GET['page']:1;
$pagesize = isset($_GET['pagesize'])? $_GET['pagesize']:1;
if(!is_numeric($pagesize) || !is_numeric($page)){
return Response::show(401,"数据不合法");
}
$offset = ($page - 1)*$pagesize;
$sql = "select * from geng_content Limit ".$offset.",".$pagesize;
try {
$connect = Db::getInstance()->connect();
} catch (Exception $e) {
return Response::show(403,"数据库连接失败");
}
$result = mysql_query($sql,$connect);
$contents = array();
while($content = mysql_fetch_assoc($result)){
$contents[] = $content;
}
if($contents){
return Response::show(200,"数据获取成功",$contents);
}else{
return Response::show(400,"数据获取失败");
}
用到的是异常抛出,list中来捕获,自定义返回提示信息,防止更多的信息被用户知晓。
以上内容有参考暮课网教学视频《PHP开发APP接口》