PHP MYSQL

CREATE A REST API IN PHP & MYSQL

(quick guide & example)

id       | INT PRIMARY email | VARCHAR UNIQUE name | VARCHAR NOT NULL

DUMMY USERS TABLE

01

class Users {   CONNECT TO DATABASE   function __construct () {     $this->pdo = new PDO("mysql:host=     HOST;dbname=NAME;charset=utf8",     USER, PASSWORD);   }

PHP USERS LIBRARY

02

  GET USER   function get ($id)  {     $stmt = $this->pdo->prepare("SELECT *     FROM `users` WHERE `id`=?");     $stmt->execute([$id]);      return $stmt->fetch();    } }

EXAMPLE - $_POST["ID"]=123 TO HTTP://API.SITE.COM/GETUSER

LOAD LIBRARY require "LIB.PHP"; $LIB = new Users();

API ENDPOINT

03

GET USER & RESPOND $user = $LIB->get($_POST["id"]); echo json_encode(["status" => 1, "user” => $user]);