CRUD OPERATION REST API using PHP and MYSQLI

how to make CRUD OPERATION REST API using PHP and MYSQLI, this tutorial we will discuss about CRUD OPERATION REST API using PHP and MYSQLI. The API is a file for storing data from a database in json format. Here we use the PHP programming language to create the API.

In this tutorial we will discuss about CRUD OPERATION REST API using PHP and MYSQLI. The API is a file for storing data from a database in json format. Here we use the PHP programming language to create the API.

Before entering the discussion, make a database with the name of the database and make a table like this.

DATABASE


CREATE TABLE `biodata` (
  `id` int(11) NOT NULL,
  `nama` varchar(65) NOT NULL,
  `kelas` varchar(35) NOT NULL
`alamat` varchar(105) NOT NULL,
`jurusan` varchar(65) NOT NULL,
`pangkat` varchar(35) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `biodata`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `biodata`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=25;


Then enter some data like the following.


INSERT INTO `biodata` (`id`, `nama`, `kelas `,` alamat `,`jurusan`,`pangkat`) VALUES
(1, 'Erji, 'XII’,’pasar’,’software engineering’,’ketua kelas’),
(2, ridho, 'XII’,’lahat’,’software engineering’,’wakil ketua’),
(3, lubis, 'XII’,’palembang’,’software engineering’,’siswa’),
(4, horison, 'XII’,’lahat’,’software engineering’,’guru’);


After creating the database as above, we will create an add.php file. The function of the add.php file is to create a function to add data to the database. Please create the file then save it into a folder on the local server (localhost). Then enter the following php code into the php file that was created earlier.

add.php


<?php
    require_once('connection.php');
    
    if(isset($_POST['nama'])&&isset($_POST['kelas'])&&isset($_POST['alamat'])&&isset($_POST['jurusan'])&&isset($_POST['pangkat'])){        
    $nama    = $_POST['nama'];
    $kelas   = $_POST['kelas'];
    $alamat  = $_POST['alamat'];
    $jurusan = $_POST['jurusan'];
    $pangkat = $_POST['pangkat'];
    
    // Query untuk Insert
    $SQL = $conn -> prepare("INSERT INTO biodata (nama, kelas, alamat, jurusan, pangkat) VALUES (?,?,?,?,?)");
    $SQL -> bind_param("sssss",$nama,$kelas,$alamat,$jurusan,$pangkat);
    $SQL -> execute(); 
    
    if($SQL) {
        echo json_encode(array( 'RESPONSE'=>'SUCCESS' ));
    } else {
        echo json_encode(array( 'RESPONSE'=>'FAILED' ));
    }
    } else {
        echo "GAGAL";
    }
?>


Then make a php file again with the name connection.php and save it in the same folder. Then open the file and enter the script as below.

connection.php


<?php

define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','biosiswa');
$conn = new mysqli(HOST,USER,PASS,DB) or die('Connetion error to the database');

date_default_timezone_set("ASIA/JAKARTA");
?>


Next is to create the list.php file. The function of this file is to display data from the database in json format. Please create a list.php file and enter the following script in it.

list.php


<?PHP 
    require_once('connection.php');
    
    //Query untuk Tampil 
    $SQL = mysqli_query($conn, "SELECT * FROM biodata ORDER BY id ASC");
    
    $result = array();
    while($row = mysqli_fetch_array($SQL)){
        ARRAY_PUSH($result,array(
            'id'    => $row['id'],
            'nama'        => $row['nama'],
            'kelas'        => $row['kelas'],
            'alamat'    => $row['alamat'],
            'jurusan'        => $row['jurusan'],
            'pangkat'        => $row['pangkat']
        ));
    }
    echo json_encode(array($result));

    mysqli_close($conn);

 ?>


The next step is to create a single.php file. The function of this file is to display a data based on the id input by the user. Please create a single.php file and enter the following script in it.

single.php


<?PHP 
    require_once('connection.php');
    
    if(isset($_POST['id'])) {
    $id = $_POST['id'];
    
    //Query untuk Tampil 
    $SQL = $conn -> prepare("SELECT * FROM biodata WHERE id=? ORDER BY id ASC");
    $SQL -> bind_param("i",$id);
    $SQL -> execute(); 
    $hasil = $SQL->get_result();
    $users = $hasil->fetch_all(MYSQLI_ASSOC);

    foreach($users as $key =>$seluruhhasil) {
        echo json_encode(array($seluruhhasil));
    }
    } else {
    echo "data tidak ditemukan";
    
}
 ?>


Then make an update.php file. and enter the following script in the file.

update.php


<?php
    require_once('connection.php');
    
    if(isset($_POST['id'])&&isset($_POST['nama'])&&isset($_POST['kelas'])&&isset($_POST['alamat'])&&isset($_POST['jurusan'])&&isset($_POST['pangkat'])){
    $id      = $_POST['id'];
    $nama    = $_POST['nama'];
    $kelas   = $_POST['kelas'];
    $alamat  = $_POST['alamat'];
    $jurusan = $_POST['jurusan'];
    $pangkat = $_POST['pangkat'];
    
    // Query untuk Insert
    $SQL = $conn -> prepare("UPDATE biodata SET nama =?, kelas=?, alamat=?, jurusan=?, pangkat=? WHERE id=?");
    $SQL -> bind_param("sssssi",$nama,$kelas,$alamat,$jurusan,$pangkat,$id);
    $SQL -> execute();    
       
    if($SQL) {
        echo json_encode(array( 'RESPONSE'=>'SUCCESS' ));
    } else {
        echo json_encode(array( 'RESPONSE'=>'FAILED' ));
    }
    } else {
        
        echo "GAGAL";
    }
?>


The last is to make the file delete.php. Please create the file and enter the script as follows.

delete.php


<?php
    require_once('connection.php');
    
    if(isset($_POST['id'])) {
        
    $id    = $_REQUEST['id'];
    
    //Query untuk Delete
    //$SQL = mysqli_query($conn, "DELETE FROM biodata WHERE id='$id' ");

    $SQL = $conn -> prepare("DELETE FROM biodata WHERE id=?");
    $SQL -> bind_param("i",$id);
    $SQL -> execute(); 
    
    if($SQL) {
        echo json_encode(array( 'RESPONSE'=>'SUCCESS' ));
    } else {
        echo json_encode(array( 'RESPONSE'=>'FAILED' ));
    }
    } else {
        echo "GAGAL";
    }
?>


After all the files above are made, don't forget to save and test. Here we will use the Postman application for checking the fire that we made above well running or not.

Please open the Postman application and please test. Following are the results of the API test that we made above using the Postman application.

list.php

add.php

update.php

 delete.php

single.php

So for this tutorial, if there is a mistake in writing please forgive and if there is an error can ask through comments. Please study and practice it to be more useful. thanks.

Author : Nur Muhammad Erji Ridho Lubis
Website : www.portalcoding.com
Theme : Programming tutorials

COMMENTS


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: CRUD OPERATION REST API using PHP and MYSQLI
CRUD OPERATION REST API using PHP and MYSQLI
how to make CRUD OPERATION REST API using PHP and MYSQLI, this tutorial we will discuss about CRUD OPERATION REST API using PHP and MYSQLI. The API is a file for storing data from a database in json format. Here we use the PHP programming language to create the API.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3d0Ay_tILbcAcW1TKVV1HR_XjgVkdxFuWI6hnGuKsOqW-yljt8BsNJtDlkhnN5m9NoAMllnAxQQ6q2iSZSIai8azq7UOV89k74-gcRUGQqF8LermZrmetmwipvLmcTkOIEXPRCw-rBUQ/s320/CRUD-OPERATION-REST-API-using-PHP-and-MYSQLI-1.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3d0Ay_tILbcAcW1TKVV1HR_XjgVkdxFuWI6hnGuKsOqW-yljt8BsNJtDlkhnN5m9NoAMllnAxQQ6q2iSZSIai8azq7UOV89k74-gcRUGQqF8LermZrmetmwipvLmcTkOIEXPRCw-rBUQ/s72-c/CRUD-OPERATION-REST-API-using-PHP-and-MYSQLI-1.PNG
KODE AJAIB
https://scqq.blogspot.com/2018/09/crud-operation-rest-api-using-php-and.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2018/09/crud-operation-rest-api-using-php-and.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy