✔ Crud Operation Rest Api Using Php And Mysqli
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.
Then enter some data like the following.
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.
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.
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.
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.
Then make an update.php file. and enter the following script in the file.
The last is to make the file delete.php. Please create the file and enter the script as follows.
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.
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 Sumber http://scqq.blogspot.com
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 Sumber http://scqq.blogspot.com
Nagaqq Yang Merupakan Agen Bandarq terbaik , Domino 99, Dan Bandar Poker Online Terpercaya di asia hadir untuk anda semua dengan permainan permainan menarik dan bonus menarik untuk anda semua
BalasHapusBonus yang diberikan NagaQQ :
* Bonus rollingan 0.5%,setiap senin di bagikannya
* Bonus Refferal 10% + 10%,seumur hidup
* Bonus Jackpot, yang dapat anda dapatkan dengan mudah
* Minimal Depo 15.000
* Minimal WD 20.000
* Deposit via Pulsa TELKOMSEL
* 6 JENIS BANK ( BCA , BNI, BRI , MANDIRI , CIMB , DANAMON )
Memegang Gelar atau title sebagai AGEN POKER ONLINE Terbaik di masanya
11 Games Yang di Hadirkan NagaQQ :
* Poker Online
* BandarQ
* Domino99
* Bandar Poker
* Bandar66
* Sakong
* Capsa Susun
* AduQ
* Perang Bacarrat
* Perang Dadu
* BD QQ (New Game)
Info Lebih lanjut Kunjungi :
Website : NAGAQQ
Facebook : NagaQQ official
WHATSAPP : +855977509035
Line : Cs_nagaQQ
TELEGRAM :+855967014811
BACA JUGA BLOGSPORT KAMI YANG LAIN:
Winner NagaQQ
Daftar NagaQQ
nagaqq