Home > Uncategorized > Generate JSON from MySQL+PHP

Generate JSON from MySQL+PHP

Format data JSON adalah salah satu pilihan yang banyak digunakan para programmer.
Berikut sebuah contoh, hasil query MySQL dikonversi menjadi JSON format.

$array = array();
$SQL="SELECT id, name FROM tblStudent";
$hasil = mysql_query($SQL);
	if($hasil) {
	  while ($row=mysql_fetch_assoc($hasil)) {
	        array_push($array,$row);
	  }
	}
$json_array = json_encode($array);
echo "{\"student\":".$json_array."}";

hasilnya sebagai berikut:

{"student":[
  {"id":"1401010002","name":"DWI LESTARI"},
  {"id":"1401010003","name":"YOANITA YELIANE"}
]} 
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: