Example Convert mysql to json in PHP

for a web developer, Converting mysql to json in PHP is one of the most important tasks to learn, 
because currently JSON has gained enormous popularity and also prefer XML as a data exchange format between web and orroid apps. 

The JSON format itself also has its own advantages such as lightness, the ability to store complex data structures in plain text and can be read easily,  not infrequently people more often use json format than the json predecessor is xml, because xml still has many shortcomings. 
The following is the way of the conversation

create new database mysql by the name"penjualan"

create table "tbbarang"

KodeBarang int(1)  <=primary key
NamaBarang varchar(150)
HargaSatuan decimal(10,0)

the contents of the data in the table "tbbarang"

now we create php file
name it Config.php
<?php
$host="localhost";
$username="root";
$password="";
$db="penjualan";

mysql_connect($host, $username, $password) or die("GAGAL");
mysql_select_db($db) or die("database tidak ada");

$find_db = mysql_select_db($db);
?>


data.php


<?php
include "Config.php";
$query=mysql_query("select * from tbbarang");
$jumlah=mysql_num_rows($query);
echo "Jumlah data ada : ".$jumlah;
?>
<table border="1" cellspacing="0">
<tr>
<th>Kode Barang</th>
<th>Nama Barang</th>
<th>Harga Satuan</th>
</tr>

<?php
while($row=mysql_fetch_array($query)){
?>
<tr>
<td><?php echo $row['KodeBarang'];?></td>
<td><?php echo $row['NamaBarang'];?></td>
<td><?php echo $row['HargaSatuan'];?></td>
<td>
<a href="delete.php?KodeBarang=<?php echo $row['KodeBarang']; ?>" onclick="return confirm('Apakah anda
yakin?')"><img src="hapus.png" width="20" height="20" /></a>
<a href="update.php?KodeBarang=<?php echo $row['KodeBarang']; ?>"><img src="edit.png" width="20" height="20" /></a>
</td>
<?php
}
?>
</table><br />
<a href="index.php">Tambah Data</a><br><br>

delete.php


<?php
include "Config.php";
$KodeBarang=$_GET['KodeBarang'];
$query=mysql_query("delete from tbbarang where KodeBarang='$KodeBarang'");
if($query){
?><script language="javascript">document.location.href="data.php";</script><?php
}else{
echo "gagal hapus data";
}
?>

index.php


<html>
      <head>
            <title>Tugas Android </title>
            <style type="text/css">
            .labelfrm{
                  display:block;
                  font-size:small;
                  margin-top:5px;
            }
            .error{
                  font-size:small;
                  color:red;
            }
            </style>
      </head>
      <body>
      <table align="center">
            <h1>Input Data Barang</h1>
     
            <form action="input.php" method="post" id="frm">
                  <label for="NamaBarang" class="labelfrm" >Nama Barang : </label>
                  <input type="text" name="NamaBarang" id="NamaBarang" size="30" class="required"/>
                  <label for="HargaSatuan" class="labelfrm">Harga Satuan : </label>
                  <textarea name="HargaSatuan" id="HargaSatuan" cols="40" rows="4" class="required"></textarea>
                  <label for="submit" class="labelfrm">&nbsp;</label>
                  <input type="submit" name="input" value="Simpan" id="input"/>
                  <input type="reset" name="clear" value="clear" id="clear"/>
            </form>
      <br><br>
            <a href="data.php">Lihat Data</a>  
      </table>
</html>

input.php


<?php
      include "Config.php"; 
      $KodeBarang = $_POST['KodeBarang'];
      $NamaBarang= $_POST['NamaBarang'];
      $HargaSatuan = $_POST['HargaSatuan'];
      $simpan = mysql_query("Insert Into tbbarang values('$KodeBarang','$NamaBarang','$HargaSatuan')");
      header('location:data.php');
?>

Service.php

<?php
require_once "config.php";
$query ="SELECT * FROM tbbarang";
$result = mysql_query($query) or die ('Errorquery: '.query);

$row = array();
while($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}
$data ="(Data:".json_encode($rows).")";
echo $data;
?>

simpan.php

<?php
include "Config.php";
$id=$_POST['KodeBarang'];
$NamaBarang=$_POST['NamaBarang'];
$HargaSatuan=$_POST['HargaSatuan'];

$query=mysql_query("update tbbarang set NamaBarang='$NamaBarang', HargaSatuan='$HargaSatuan' where KodeBarang='$id'");
if($query){
header ('location:data.php');
?> 
<?php
}else{
echo "Gagal update data";
echo mysql_error();
}
?>

update.php

<?php
include "Config.php";
$KodeBarang=$_GET['KodeBarang'];
$query=mysql_query("select * from tbbarang where KodeBarang='$KodeBarang'");
?>
<form action="simpan.php" method="post">
<table border="1">
<h1>Edit Data Barang</h1>
<?php
while($row=mysql_fetch_array($query)){
?>
<input type="hidden" name="KodeBarang" value="<?php echo $KodeBarang;?>"/>
<tr>
<td>Nama Barang</td><td><input type="text" name="NamaBarang" value="<?php echo $row['NamaBarang'];?>" /></td>
</tr>
<tr>
<td>Harga Satuan</td>
<td><textarea cols="20" rows="5" name="HargaSatuan"><?php echo $row['HargaSatuan'];?></textarea></td>
</tr>
<tr><td><input type="submit" value="Simpan" name="simpan" /></td>
</tr>
<?php
}
?>
</table>
</form>


all php files above are stored in xampp, create a new folder in the xampp, and save all the php files in it

add two icon images with edit and delete names.

then you enable xampp, start apache and mysql.
run the browser, and typing commends localhost/folderName files php

the contents of the data into the database "penjualan". 

then type the command in the browser, localhost/folderName/service.php and enter.

output :

Posting Komentar untuk " Example Convert mysql to json in PHP"