Posts

Search Data with REST API in PHP

Image
  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $database = "test" ; //Create Connection $conn = new mysqli ( $servername , $username , $password , $database ); //Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed: " . $conn -> connect_error ); } Above File is mysqli_conn.php which is included in below files. Below file is api-search.php File. <?php header ( "Content-Type: application/json" ); header ( "Access-Control-Allow-Origin: *" ); // $data = json_decode(file_get_contents("php://input"), true); // $search_value = $data["search"]; //Search through URL as shown below : $search_value = isset ( $_GET [ "search" ]) ? $_GET [ "search" ] : die (); include ( "mysqli_conn.php" ); $sql = " SELECT * FROM students WHERE student_name LIKE '%{ $search_value }%' " ; $result = $c...

Delete Data with REST API in PHP

Image
  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $database = "test" ; //Create Connection $conn = new mysqli ( $servername , $username , $password , $database ); //Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed: " . $conn -> connect_error ); } Above File is mysqli_conn.php which is included in below Files. Below File is api-delete.php File. <?php header ( "Content-Type: application/json" ); header ( "Access-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" ); //Above "X-Requested-With" means Whatever values comes for insert in database must with Ajax $data = json_decode ( file_get_contents ( "php://input" ), true ); $id = $data...

Update Data with REST API in PHP

Image
  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $database = "test" ; //Create Connection $conn = new mysqli ( $servername , $username , $password , $database ); //Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed: " . $conn -> connect_error ); } Above File is mysqli_conn.php which is included in below Files. Below File is api-update.php File. <?php header ( "Content-Type: application/json" ); header ( "Access-Control-Allow-Origin: *" ); header ( "Access-Control-Allow-Methods: PUT" ); header ( "Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization, X-Requested-With" ); //Above "X-Requested-With" means Whatever values comes for insert in database must with Ajax $data = json_decode ( file_get_contents ( "php://input" ), true ); $id = $data [ ...

Insert Data with REST API in PHP

Image
  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $database = "test" ; //Create Connection $conn = new mysqli ( $servername , $username , $password , $database ); //Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed: " . $conn -> connect_error ); } Above File is mysqli_conn.php File which is included in below Files. Below File is api-insert.php File. <?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" ); //Above "X-Requested-With" means Whatever values comes for insert in database must with Ajax $data = json_decode ( file_get_contents ( "php://input" ), true ); $name = ...

Read Data with REST API in PHP

Image
  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $database = "test" ; //Create Connection $conn = new mysqli ( $servername , $username , $password , $database ); //Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed: " . $conn -> connect_error ); } Above File is mysqli_conn.php File which is included in below Files. Below is api-fetch-all.php File. <?php header ( "Content-Type: application/json" ); header ( "Access-Control-Allow-Origin: *" ); include ( "mysqli_conn.php" ); $sql = " SELECT * FROM students " ; $result = $conn -> query ( $sql ); if ( $result -> num_rows > 0 ) {     //The fetch_all() / mysqli_fetch_all() function fetches all result rows and returns the result-set as an associative array, a numeric array, or both.     $output = $result -> fetch_all (MYSQLI_ASSOC); //Fetch all rows and return the...

Submitting HTML Form data in JSON File (without MySql Database) in PHP

Image
  <! DOCTYPE html > < html lang = "en" > < head >     < meta charset = "UTF-8" >     < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >     < meta http-equiv = "X-UA-Compatible" content = "ie=edge" >     < title > PHP & JSON File </ title >     < link rel = "stylesheet" href = "css/style.css" > </ head > < body >     < div id = "main" >         < div id = "header" >             < h1 > Save Form Data in JSON File </ h1 >         </ div >         < div id = "table-data" >             < form id = "submit_form" method = "post" action = "save-form.php" >                 < table width = "100%" cellpadding = "10px" ...

Create Dynamic JSON File in PHP

Image
  <?php $servername = "localhost" ; $username = "root" ; $password = "" ; $database = "test" ; //Create Connection $conn = new mysqli ( $servername , $username , $password , $database ); //Check Connection if ( $conn -> connect_error ) {     die ( "Connection Failed: " . $conn -> connect_error ); } Above file is mysqli_conn.php File which is included in below files. Below is daily-json-file.php File <?php include ( "mysqli_conn.php" ); $sql = " SELECT * FROM students " ; $result = $conn -> query ( $sql ); $output = $result -> fetch_all (MYSQLI_ASSOC); $json_data = json_encode ( $output , JSON_PRETTY_PRINT); $file_name = "my-" . date ( "d-m-Y" ) . ".json" ; if ( file_put_contents ( "json/{ $file_name }" , $json_data )) {     echo $file_name . "File created." ; } else {     echo "Can't Insert data in JSON File." ...