Backend API and database examples

This document provides examples of how to write backend PHP code within the [h5_php] folder of the ShipLang framework.

Backend API Example

[Db::input] can read parameters from both POST and GET requests.

The database username and password are set in the folder [shiplang]->[db.php].

[Db::return_json_exit] returns JSON and supports translating the returned result. For detailed documentation, see: https://shiplang.com/zh-cn/dosc/lang-api

The [h5_php] folder contains the PHP backend folder. API methods within this folder must be named using the [api_] prefix to be considered API interfaces.

[h5_php] and [h5_html] are in the same directory.

header('Content-Type: text/html; charset=UTF-8');
require_once($_SERVER['DOCUMENT_ROOT'] . '/shiplang/db.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/shiplang/lang.php');
class NbHtml {
    public function api_test() {
        $id = Db::input('id','');//假设:1.com/a/api_test?id=1,那么input就是读取链接里的id
        if (empty($id)) {
            Db::return_json_exit(['code' => 400,'msg' => '请输入id']);
        }
        
        Db::execute("INSERT INTO aaa (id,bbb) VALUES (?,?)",['1','bbb']);//增
        Db::execute("DELETE FROM aaa WHERE id = ?", ['1']);//删
        Db::execute("UPDATE aaa SET bbb = ? WHERE id = ?",['new_bbb','id']);//改
        $user = Db::query("SELECT * FROM aaa WHERE id = ?", [$id]);//查 使用query
        
        Db::return_json_exit(['code' => 200,'msg' => '查询结果','data' => [
            'user' => $user
        ]]);
    }
}