0% found this document useful (0 votes)
29 views

Code WB Serv

Uploaded by

IstianahBuis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Code WB Serv

Uploaded by

IstianahBuis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

SOURCE CODE

Koneksi.php
<?php
$mysqli = new mysqli("localhost", "root", "", "db_ws");
if ($mysqli->connect_errno){
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .
$mysqli->connect_error;
}
?>

Index.php
<?php
//koneksi ke database
include "koneksi.php";
//proses client request(VIA URL)
header("Content-Type:application/json");
if(!empty($_GET['nim'])){
//Ambil data nim dari URL
$nim=$_GET['nim'];
//cek ke database
$res = $mysqli->query("SELECT * FROM mahasiswa WHERE nim = '".$nim."'");
//cek jika data di temukan di database
if($res->num_rows>0){
$row= $res->fetch_array();
//tampilkan dalam bentuk array
$data=array('nim'=>$row['nim'], 'nama'=>$row['nama'], 'jurusan'=>$row['jurusan']);
//panggil fungsi respon
deliver_response(200,"Mahasiswa ditemukan", $data);
}else {
//panggil fungsi respon
deliver_response(200,"Mahasiswa tidak ditemukan", NULL);
}
}else{
//panggil fungsi respon
deliver_response(400,"Invalid Request", NULL);
}
//fungsi respon
function deliver_response($status, $status_message, $data){
header("HTTP/1.1 $status $status_message");
//array
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response=json_encode($response);
echo $json_response;
}
?>

You might also like