PHP MYSQL

REST API WITH PHP MYSQL - OVERLY SIMPLIFIED EXAMPLE

SIMPLE USERS TABLE email | varchar(255) PRIMARY name | varchar(255)

DATABASE TABLE

01

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

PHP LIBRARY CLASS

02

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

EXAMPLE: $_POST["email"] = "joe@doe.com" TO http://site.com/API/GETUSER/

CHECK ACCESS PERMISSION session_start(); if (!isset($_SESSION["user"]) { exit("NO"); }

API ENDPOINT

03

GET USER & RESPOND $USR = new Users();  $user = $USR->get($_POST["email"]); echo json_encode($user);