接口集合

// 链接数据库接口--conn.php
<?php
    @header("Content-Type:text/html;charset=utf-8");

    // php链接mysql 

    // 1. 链接mysql 

    const host = "localhost:3306";  // 主机名
    const user = "root";  // 用户名
    const pwd = "root";   // 密码
    const dbName = "2101";   // 密码

    // $conn = mysqli_connect(host,user,pwd);  // 如果链接成功  返回一个链接对象(php中的对象)  失败:报错
    // print_r($conn);

    // 2. 选择数据库
    // $res = mysqli_select_db($conn,dbName);    // 如果成功 返回true 失败返回false

    $conn = mysqli_connect(host,user,pwd,dbName);
    // if($res){
    //     echo "数据库链接成功";
    // }else{
    //     echo "数据库链接失败";
    // }
 
    if(!$conn){  // $res == false 数据库链接失败  =>  阻止脚本继续向后执行
        exit("数据库链接失败");
    }

    // 设置编码格式     (兼容低版本)
     mysqli_query($conn,"set name utf8");  // 从数据库取数据时  将编码转为utf8;   
     mysqli_query($conn,"set character set utf-8");  //  向数据库存数据时  将编码转为utf8; 
 
// 验证注册接口--isRight.php
<?php
@header("Content-Type:text/html;charset=utf-8");

// 封装一个报错的函数
function err(){
    $msg = array();
    $msg["status"] = false;
    $msg["detail"] = "请传入完整参数";
    exit(json_encode($msg));
}

// 封装一个判断注册信息的函数
function isRight($key){
    // 允许局部变量访问全局变量
    global $conn; 
    // 根据字段名获取对应的值
    $val = $_GET[$key];
    // echo $val;
    // 没有输入就报错
    if(!$val){ 
        err();
    }
    // 查询键值对
    $search = "select * from `userinfo` where $key = '$val'";
    // 根据键值对查询数据库
    $result = mysqli_query($conn, $search);
    // 解析查询结果
    $item   = mysqli_fetch_assoc($result);

    // 创建一个关联数组,添加键名
    $list = array("user" => "用户名","phone"=>"电话","email"=>"邮箱");

    $msg  = array();
    if(!$item){
        $msg["status"] = true;
        $msg["detail"] = "可以使用的".$list[$key]; // 根据前端传入的键名取出文字
    }else{
        $msg["status"] = false;
        $msg["detail"] = $list[$key]."已注册";
    }
    echo json_encode($msg);
}
// 注册接口 --register.php
<?php
@include_once("conn.php");
@include_once("isRight.php");

// 判断前端是否传入了值,传入了就验证
if(isset($_GET["user"])){
    isRight("user");
}else if(isset($_GET["phone"])){
    isRight("phone");
}else if(isset($_GET["email"])){
    isRight("email");
}else{
    err();
}

// 注册数据插入数据库接口 --insert.php
<?php
 @include_once("conn.php"); // 引入公共的php
 @include_once("isRight.php");

 $user  = $_GET["user"];
 $pwd   = $_GET["pwd"];
 $phone = $_GET["phone"];
 $email = $_GET["email"];
 
 if(!($user&&$pwd&&$phone&&$email)){   // 如果不传入完整参数  => 提示(且阻止脚本继续向后执行)
   err();
}


// 插入注册的信息
$insert = "insert into `userinfo`(user,pwd,phone,email) values('$user','$pwd','$phone','$email')";
mysqli_query($conn,$insert);

// 数据库受影响的行数
$rows = mysqli_affected_rows($conn);

$msg = array();
    if($rows >0){
        $msg["status"] = true;   
        $msg["detail"] = "新增成功";   
    }else{
        $msg["status"] = false;   
        $msg["detail"] = "新增失败";   
        $msg["sql"] = $insert;   
    }

    echo json_encode($msg);
// 登录接口 -- login.php
<?php
// 引入公共php
@include_once("conn.php");
@include_once("isRight.php");

// 对应字段名
$account = $_POST["account"];
$pwd  = $_POST["pwd"];
// 有一个未接收到,报错
if(!($account&&$pwd)){
    err();
}

// 查询
$search = "select * from `userinfo` where user = '$account' or phone = '$account' or email = '$account'";

// 查询数据库
$result = mysqli_query($conn, $search);

// 解析结果
$item = mysqli_fetch_assoc($result);

// 创建一个空数组
$msg = array();
if($item){
    // 根据解析结果找到真正的密码
    $realPwd = $item["pwd"];
    if($realPwd === $pwd){
        $msg["status"] = true;
        $msg["detail"] = "登录成功";
        // 给前端返回user展示
        $msg["user"]   = $item["user"];
    }else{
        $msg["status"] = false;
        $msg["detail"] = "密码有误";
    }
}else{
    $msg["status"] = false;
    $msg["detail"] = "该用户未注册";
}

echo json_encode($msg);

// 删除接口 -- deleteGrade.php
<?php
    // 1.连接数据库
    @include_once("conn.php"); // 引入公共的php
    @include_once("isRight.php");

    // 2.接受前端的字段名
    $id = $_GET["id"];        
    if(!$id){   
       err();
    }

    // 3.链接查询语句
    $del = "delete from `grade` where id in ('$id')";   
    mysqli_query($conn,$del);
    
    // 数据库受影响的行数
    $rows = mysqli_affected_rows($conn);
    
    $msg = array();
    if($rows >0){
        $msg["status"] = true;   
        $msg["detail"] = "删除成功";   
    }else if($rows == 0){
        $msg["status"] = false;   
        $msg["detail"] = "该数据已被删除";   
    }else{
        $msg["status"] = false;   
        $msg["detail"] = "删除失败,语法有误";   
        $msg["sql"] = $del;   
    }

    echo json_encode($msg);


// 根据id查询数据的接口 -- searchGradeById.php
<?php
@include_once("conn.php");
@include_once("isRight.php");

// 对应id的字段名
$id = $_GET["id"];
// 报错
if(!$id){
    err();
}

// 查询语句
$search = "SELECT id,NAME,class,chinese,math,english,chinese+math+english AS total FROM `grade` where id='$id'";

// 链接数据库
$result =  mysqli_query($conn, $search);

// 解析数据
$item = mysqli_fetch_assoc($result);

// 数据处理
$msg = array();
if($item){
    $msg["status"] = true;
    $msg["detail"] = "查询成功";
    $msg["data"]   = $item;
}else{
    $msg["status"] = false;
    $msg["detail"] = "查询失败";
    $msg["data"]   = null;
}

echo json_encode($msg);

//根据id更新数据的接口 -- updateGradeById.php
<?php
@include_once("conn.php");
@include_once("isRight.php");

// 对应id的字段名
$id = $_GET["id"];
// 报错
if(!$id){
    err();
}

// 查询语句
$search = "SELECT id,NAME,class,chinese,math,english,chinese+math+english AS total FROM `grade` where id='$id'";

// 链接数据库
$result =  mysqli_query($conn, $search);

// 解析数据
$item = mysqli_fetch_assoc($result);

// 数据处理
$msg = array();
if($item){
    $msg["status"] = true;
    $msg["detail"] = "查询成功";
    $msg["data"]   = $item;
}else{
    $msg["status"] = false;
    $msg["detail"] = "查询失败";
    $msg["data"]   = null;
}

echo json_encode($msg);

//前端搜索,排序和限制页数的接口 -- searchAllGradesOrder.php
<?php
@include_once("conn.php");
@include_once("isRight.php");


$key = isset($_GET["key"]) ? $_GET["key"] : "";   // 是否存在字段名 key  存在则接收   不存在报错
$orderCol = isset($_GET["orderCol"]) ? $_GET["orderCol"] : "id";
$orderType = isset($_GET["orderType"]) ? $_GET["orderType"] : "asc";



// 查询语句
$search = "select id,NAME,class,chinese,math,english,chinese+math+english as total from `grade` where NAME like '%$key%' order by $orderCol $orderType";

// 链接数据库
$result = mysqli_query($conn, $search);

$list = array();
// 数据过多,循环解析
while ($item = mysqli_fetch_assoc($result)) {
    array_push($list, $item);
}

echo json_encode($list);

// 后端搜索,排序和限制页数的接口 -- searchGradeOrderLimit.php
<?php
@include_once("conn.php");
@include_once("isRight.php");

// 对应字段名
$key = $_GET["key"]; //              =>搜索的关键词
$orderCol = $_GET["orderCol"]; //    =>排序的列名 
$orderType = $_GET["orderType"]; //  =>排序的方式 (asc desc)
$pageIndex = $_GET["pageIndex"];  // =>页码
$showNum = $_GET["showNum"];      // =>每页显示多少条

// 报错
// if (!($key && $orderCol && $orderType && $pageIndex && $showNum)) {
//     err();
// }

// 查询
$searchAll = "select count(*) as count from `grade` where NAME like '%$key%'";
$result = mysqli_query($conn, $searchAll);
$item = mysqli_fetch_assoc($result);

// 拿出解析结果的总数量
$count = $item["count"] * 1;

// 最大页数
// $showNum = 5;
$maxPage = ceil($count / $showNum);
// echo $maxPage;

// 限流
if ($pageIndex > $maxPage) {
    $pageIndex = $maxPage;
}
if ($pageIndex < 1) {
    $pageIndex = 1;
}
// 后端分页
    // $pageIndex   $showNum 
    // 1    limit 0,5
    // 2    limit 5,5
    // 3    limit 10,5
    // 4    limit 15,5

    //      limit     ($pageIndex-1)*$showNum  , $showNum
// 跳过的数据
$skipNum = ($pageIndex - 1) * $showNum;

// 查询
$search = "select id,NAME,class,chinese,math,english,chinese+math+english as total from `grade` where NAME like '%$key%' order by $orderCol $orderType limit $skipNum,$showNum";
$res = mysqli_query($conn, $search);

$msg = array();
// 多数据解析,放到空数组中
while ($item = mysqli_fetch_assoc($res)) {
    array_push($msg, $item);
}

$obj = array();
if ($msg) {
    $obj["status"] = true;
    $obj["detail"] = "查询成功";
    // 提示 pageIndex/maxPage
    $obj["maxPage"] = $maxPage;
    // 裁切
    $obj["count"] = $count;
    // 渲染
    $obj["list"] = $msg;
}else{
    $obj["status"] = false;
    $obj["detail"] = "查询失败";
    $obj["sql"]    = $search;
}


echo json_encode($obj);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值