Posts

Showing posts from February, 2023

Create a REST API with Node js & Express

 app.js:-  const express = require ( "express" ); const http = require ( "http" ); require ( "./db/connection" ); const router = require ( "./api/emp/index" ) const employee = require ( "./api/emp/model" ); const app = express (); const server = http . createServer ( app ); const port = process . env . PORT || 3000 ; const ip = process . env . IP || "localhost" ; app . use ( express . json ()) app . use ( router ) server . listen ( port , ip , () => {     console . log ( "Express Server Listening on http://%s:%d" , ip , port ); }) --------------------------------- connection.js: const mongoose = require ( 'mongoose' ); mongoose . connect ( 'mongodb://127.0.0.1:27017/EMPDB' ). then (() => {     console . log ( "MongoDb Connection Sucessfull...." ); }). catch (( e ) => {     console . log ( "Mongodb connection Failed!!!" ); }) ------------------------------ Model

PHP Rest Search API through GET Method

 api-search.php: <?php header ( 'Content-Type: application/json' ); header ( 'Acess-Control-Allow-Origin: *' ); // $data=json_decode(file_get_contents("php://input"), true); // $search_value=$data['search']; $search_value = isset ( $_GET [ 'search' ]) ? $_GET [ 'search' ] : die (); include "config.php" ; $sql = " SELECT * FROM students WHERE student_name LIKE '%{ $search_value }%' " ; $result = mysqli_query ( $conn , $sql ) or die ( "SQL query failed" ); if ( mysqli_num_rows ( $result )> 0 ){     $output = mysqli_fetch_all ( $result , MYSQLI_ASSOC);     echo json_encode ( $output ); } else {     echo json_encode ( array ( 'message' => 'NO Search FOUND' , 'status' => false )); } ? >

PHP Rest Search API through Post Method

 api-search.php: <?php header ( 'Content-Type: application/json' ); header ( 'Acess-Control-Allow-Origin: *' ); $data = json_decode ( file_get_contents ( "php://input" ), true ); $search_value = $data [ 'search' ]; include "config.php" ; $sql = " SELECT * FROM students WHERE student_name LIKE '%{ $search_value }%' " ; $result = mysqli_query ( $conn , $sql ) or die ( "SQL query failed" ); if ( mysqli_num_rows ( $result )> 0 ){     $output = mysqli_fetch_all ( $result , MYSQLI_ASSOC);     echo json_encode ( $output ); } else {     echo json_encode ( array ( 'message' => 'NO Search FOUND' , 'status' => false )); } ? >

PHP Rest Delete API

 api-delete.php: <?php header ( 'Content-Type: application/json' ); header ( 'Acess-Control-Allow-Origin: *' ); header ( 'Access-Control-Allow-Methods: DELETE' ); header ( 'Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization, X-Requested-With' ); $data = json_decode ( file_get_contents ( "php://input" ), true ); $student_id = $data [ 'sid' ]; include "config.php" ; $sql = " DELETE FROM students WHERE id = { $student_id } " ; if ( mysqli_query ( $conn , $sql )){     echo json_encode ( array ( 'message' => 'Student Record Deleted' , 'status' => true ));   } else {     echo json_encode ( array ( 'message' => 'Student Record Not Deleted' , 'status' => false )); } ? >

Rest Update API PHP

 api-update: <?php header ( 'Content-Type: application/json' ); header ( 'Access-Control-Allow-Origin: *' ); header ( 'Access-Control-Allow-Methods: POST' ); header ( 'Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization, X-Requested-With' ); $data = json_decode ( file_get_contents ( "php://input" ), true ); $id = $data [ 'sid' ]; $name = $data [ 'sname' ]; $age = $data [ 'sage' ]; $city = $data [ 'scity' ]; include "config.php" ; $sql = " UPDATE students SET student_name = '{ $name }', age = { $age }, city = '{ $city }' WHERE id = { $id } " ; if ( mysqli_query ( $conn , $sql )){     echo json_encode ( array ( 'message' => 'Student Record Updated' , 'status' => true )); } else {     echo json_encode ( array ( 'message' => 'Student Record Not Updated' , '

Insert Rest API PHP

Image
api-insert.php: -  <?php header ( 'Content-Type: application/json' ); header ( 'Access-Control-Allow-Origin: *' ); header ( 'Access-Control-Allow-Methods: POST' ); header ( 'Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization, X-Requested-With' ); $data = json_decode ( file_get_contents ( "php://input" ), true ); $name = $data [ 'sname' ]; $age = $data [ 'sage' ]; $city = $data [ 'scity' ]; include "config.php" ; $sql = " INSERT INTO students(student_name, age, city) VALUES ('{ $name }', '{ $age }', '{ $city }') " ; if ( mysqli_query ( $conn , $sql )){     echo json_encode ( array ( 'message' => 'Student Record Inserted' , 'status' => true )); } else {     echo json_encode ( array ( 'message' => 'Student Record Not Inserted' , 'status' => false )

PHP REST API - Fetch Data from SQL Database

 api-fetch-all.php: <?php header ( 'Content-Type: application/json' ); header ( 'Acess-Control-Allow-Origin: *' ); include "config.php" ; $sql = " SELECT * FROM students " ; $result = mysqli_query ( $conn , $sql ) or die ( "SQL query failed" ); if ( mysqli_num_rows ( $result )> 0 ){     $output = mysqli_fetch_all ( $result , MYSQLI_ASSOC);     echo json_encode ( $output ); } else {     echo json_encode ( array ( 'message' => 'NO RECORD FOUND' , 'status' => false )); } ? > ------------------------- api-fetch-single.php: <?php header ( 'Content-Type: application/json' ); header ( 'Acess-Control-Allow-Origin: *' ); $data = json_decode ( file_get_contents ( "php://input" ), true ); $student_id = $data [ 'sid' ]; include "config.php" ; $sql = " SELECT * FROM students WHERE id = { $student_id } " ; $result = mysqli_query ( $conn , $sql ) or