├── README.md ├── api ├── create.php ├── delete.php ├── read.php ├── single_read.php └── update.php ├── class └── employees.php └── config └── database.php /README.md: -------------------------------------------------------------------------------- 1 | # php-rest-api 2 | This is a step by step PHP 8 & MySQL REST API tutorial, In this tutorial i am going to share with you how to create a PHP 7 CRUD (Create, Read, Update, Delete) RESTful API with MySQL database. 3 | 4 | [Create Simple PHP 8 CRUD REST API with MySQL & PHP PDO](https://www.positronx.io/create-simple-php-crud-rest-api-with-mysql-php-pdo/) 5 | 6 | ## PHP CRUD API 7 | * `GET - http://localhost:8080/api/read.php` Fetch ALL Records 8 | * `GET - localhost:8080/api/single_read.php/?id=2` Fetch Single Record 9 | * `POST - http://localhost:8080/api/create.php` Create Record 10 | * `POST - http://localhost:8080/api/update.php` Update Record 11 | * `DELETE - localhost:8080/api/delete.php` Remove Records 12 | -------------------------------------------------------------------------------- /api/create.php: -------------------------------------------------------------------------------- 1 | getConnection(); 13 | 14 | $item = new Employee($db); 15 | 16 | $data = json_decode(file_get_contents("php://input")); 17 | 18 | $item->name = $data->name; 19 | $item->email = $data->email; 20 | $item->age = $data->age; 21 | $item->designation = $data->designation; 22 | $item->created = date('Y-m-d H:i:s'); 23 | 24 | if($item->createEmployee()){ 25 | echo 'Employee created successfully.'; 26 | } else{ 27 | echo 'Employee could not be created.'; 28 | } 29 | ?> -------------------------------------------------------------------------------- /api/delete.php: -------------------------------------------------------------------------------- 1 | getConnection(); 13 | 14 | $item = new Employee($db); 15 | 16 | $data = json_decode(file_get_contents("php://input")); 17 | 18 | $item->id = $data->id; 19 | 20 | if($item->deleteEmployee()){ 21 | echo json_encode("Employee deleted."); 22 | } else{ 23 | echo json_encode("Data could not be deleted"); 24 | } 25 | ?> -------------------------------------------------------------------------------- /api/read.php: -------------------------------------------------------------------------------- 1 | getConnection(); 10 | 11 | $items = new Employee($db); 12 | 13 | $stmt = $items->getEmployees(); 14 | $itemCount = $stmt->rowCount(); 15 | 16 | 17 | echo json_encode($itemCount); 18 | 19 | if($itemCount > 0){ 20 | 21 | $employeeArr = array(); 22 | $employeeArr["body"] = array(); 23 | $employeeArr["itemCount"] = $itemCount; 24 | 25 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 26 | extract($row); 27 | $e = array( 28 | "id" => $id, 29 | "name" => $name, 30 | "email" => $email, 31 | "age" => $age, 32 | "designation" => $designation, 33 | "created" => $created 34 | ); 35 | 36 | array_push($employeeArr["body"], $e); 37 | } 38 | echo json_encode($employeeArr); 39 | } 40 | 41 | else{ 42 | http_response_code(404); 43 | echo json_encode( 44 | array("message" => "No record found.") 45 | ); 46 | } 47 | ?> -------------------------------------------------------------------------------- /api/single_read.php: -------------------------------------------------------------------------------- 1 | getConnection(); 13 | 14 | $item = new Employee($db); 15 | 16 | $item->id = isset($_GET['id']) ? $_GET['id'] : die(); 17 | 18 | $item->getSingleEmployee(); 19 | 20 | if($item->name != null){ 21 | // create array 22 | $emp_arr = array( 23 | "id" => $item->id, 24 | "name" => $item->name, 25 | "email" => $item->email, 26 | "age" => $item->age, 27 | "designation" => $item->designation, 28 | "created" => $item->created 29 | ); 30 | 31 | http_response_code(200); 32 | echo json_encode($emp_arr); 33 | } 34 | 35 | else{ 36 | http_response_code(404); 37 | echo json_encode("Employee not found."); 38 | } 39 | ?> -------------------------------------------------------------------------------- /api/update.php: -------------------------------------------------------------------------------- 1 | getConnection(); 13 | 14 | $item = new Employee($db); 15 | 16 | $data = json_decode(file_get_contents("php://input")); 17 | 18 | $item->id = $data->id; 19 | 20 | // employee values 21 | $item->name = $data->name; 22 | $item->email = $data->email; 23 | $item->age = $data->age; 24 | $item->designation = $data->designation; 25 | $item->created = date('Y-m-d H:i:s'); 26 | 27 | if($item->updateEmployee()){ 28 | echo json_encode("Employee data updated."); 29 | } else{ 30 | echo json_encode("Data could not be updated"); 31 | } 32 | ?> -------------------------------------------------------------------------------- /class/employees.php: -------------------------------------------------------------------------------- 1 | conn = $db; 21 | } 22 | 23 | // GET ALL 24 | public function getEmployees(){ 25 | $sqlQuery = "SELECT id, name, email, age, designation, created FROM " . $this->db_table . ""; 26 | $stmt = $this->conn->prepare($sqlQuery); 27 | $stmt->execute(); 28 | return $stmt; 29 | } 30 | 31 | // CREATE 32 | public function createEmployee(){ 33 | $sqlQuery = "INSERT INTO 34 | ". $this->db_table ." 35 | SET 36 | name = :name, 37 | email = :email, 38 | age = :age, 39 | designation = :designation, 40 | created = :created"; 41 | 42 | $stmt = $this->conn->prepare($sqlQuery); 43 | 44 | // sanitize 45 | $this->name=htmlspecialchars(strip_tags($this->name)); 46 | $this->email=htmlspecialchars(strip_tags($this->email)); 47 | $this->age=htmlspecialchars(strip_tags($this->age)); 48 | $this->designation=htmlspecialchars(strip_tags($this->designation)); 49 | $this->created=htmlspecialchars(strip_tags($this->created)); 50 | 51 | // bind data 52 | $stmt->bindParam(":name", $this->name); 53 | $stmt->bindParam(":email", $this->email); 54 | $stmt->bindParam(":age", $this->age); 55 | $stmt->bindParam(":designation", $this->designation); 56 | $stmt->bindParam(":created", $this->created); 57 | 58 | if($stmt->execute()){ 59 | return true; 60 | } 61 | return false; 62 | } 63 | 64 | // UPDATE 65 | public function getSingleEmployee(){ 66 | $sqlQuery = "SELECT 67 | id, 68 | name, 69 | email, 70 | age, 71 | designation, 72 | created 73 | FROM 74 | ". $this->db_table ." 75 | WHERE 76 | id = ? 77 | LIMIT 0,1"; 78 | 79 | $stmt = $this->conn->prepare($sqlQuery); 80 | 81 | $stmt->bindParam(1, $this->id); 82 | 83 | $stmt->execute(); 84 | 85 | $dataRow = $stmt->fetch(PDO::FETCH_ASSOC); 86 | 87 | $this->name = $dataRow['name']; 88 | $this->email = $dataRow['email']; 89 | $this->age = $dataRow['age']; 90 | $this->designation = $dataRow['designation']; 91 | $this->created = $dataRow['created']; 92 | } 93 | 94 | // UPDATE 95 | public function updateEmployee(){ 96 | $sqlQuery = "UPDATE 97 | ". $this->db_table ." 98 | SET 99 | name = :name, 100 | email = :email, 101 | age = :age, 102 | designation = :designation, 103 | created = :created 104 | WHERE 105 | id = :id"; 106 | 107 | $stmt = $this->conn->prepare($sqlQuery); 108 | 109 | $this->name=htmlspecialchars(strip_tags($this->name)); 110 | $this->email=htmlspecialchars(strip_tags($this->email)); 111 | $this->age=htmlspecialchars(strip_tags($this->age)); 112 | $this->designation=htmlspecialchars(strip_tags($this->designation)); 113 | $this->created=htmlspecialchars(strip_tags($this->created)); 114 | $this->id=htmlspecialchars(strip_tags($this->id)); 115 | 116 | // bind data 117 | $stmt->bindParam(":name", $this->name); 118 | $stmt->bindParam(":email", $this->email); 119 | $stmt->bindParam(":age", $this->age); 120 | $stmt->bindParam(":designation", $this->designation); 121 | $stmt->bindParam(":created", $this->created); 122 | $stmt->bindParam(":id", $this->id); 123 | 124 | if($stmt->execute()){ 125 | return true; 126 | } 127 | return false; 128 | } 129 | 130 | // DELETE 131 | function deleteEmployee(){ 132 | $sqlQuery = "DELETE FROM " . $this->db_table . " WHERE id = ?"; 133 | $stmt = $this->conn->prepare($sqlQuery); 134 | 135 | $this->id=htmlspecialchars(strip_tags($this->id)); 136 | 137 | $stmt->bindParam(1, $this->id); 138 | 139 | if($stmt->execute()){ 140 | return true; 141 | } 142 | return false; 143 | } 144 | 145 | } 146 | ?> 147 | 148 | -------------------------------------------------------------------------------- /config/database.php: -------------------------------------------------------------------------------- 1 | conn = null; 12 | try{ 13 | $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->database_name, $this->username, $this->password); 14 | $this->conn->exec("set names utf8"); 15 | }catch(PDOException $exception){ 16 | echo "Database could not be connected: " . $exception->getMessage(); 17 | } 18 | return $this->conn; 19 | } 20 | } 21 | ?> --------------------------------------------------------------------------------