[Tutorial] - MySQL to JSON Data Using PHP
|
Encoding JSON in PHP
Some years ago, JSON support was provided through the json pecl extension. Since PHP 5.2, it is included in the core directly, so if you use a recent PHP version you should have no trouble using it.
Note: If you run an older version of PHP than 5.3, I recommend you to upgrade anyway. PHP 5.3 is the oldest version that is currently supported and with the latest PHP security bugs found I would consider it critical to upgrade as soon as possible.
Step 1: Create file with the naming as index.php
<?php $username = 'root'; //database user name variable $password =''; //database password $host ='localhost'; $databasename = 'json-db'; $conn = mysql_connect($host, $username, $password); if (!$conn) { die('Could not connect: ' . mysql_error()); } //select database name and connect mysql_select_db($databasename, $conn); $sth = mysql_query("Select * from users"); $rows = array(); while($r = mysql_fetch_assoc($sth)) { $rows[] = $r; } $json = json_encode($rows); echo stripslashes($json); ?>
Step 2: Retrieve JSON data (callback.php)
There are two ways to call data from another file (index.php), using JQuery or PHP (json_decode).
a. Using JQuery
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script> $(document).ready(function (){ //using getJSON $.getJSON("http://localhost/path-to-your-file/index.php", function(data){ // do stuff with json (in this case an array) alert("Success"); }); //using Ajax $.ajax({ url:"http://127.0.0.1:8888/json/index.php", dataType: 'json', // Notice! JSONP <-- P (lowercase) success:function(json){ // do stuff with json (in this case an array) alert("Success"); }, error:function(){ alert("Error"); }, }); }); </script>
b. Using PHP json_decode()
<?php $url = 'http://localhost/path-to-your-file/index.php'; $json = file_get_contents($url); $datas = json_decode($json); //var_dump($datas); foreach($datas as $data){ echo $data->last_name.' '; } ?>credited to: http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP
POSTED BY juong |
Popular Posts
-
Rumi Ke Jawi 1. Convert rumi ke jawi secara online. senang sekali! 2. Lepas covert, copy dan paste kat microsoft word, 3. dan pilih font ...
-
Tulisan jawi amat diperlukan terutama bagi guru-guru yang menggunakan tulisan jawi sebagai medium untuk membuat soalan peperiksaan dan kepad...
-
Sebelum download Arabic Dotted / titik-titik, Aracbic font perlu di install terlebih dahulu. Install arabic font di sini Kalau dah install, ...
-
Daripada Aisyah RA, Bala yang pertama sekali berlaku kepada umat ini sesudah kepergian Nabi SAW ialah kekenyangan perut, sebab apabila ses...
-
Mengetahui seseorang itu anak yang ke berapa dalam keluarga dengan menggunakan rumusan matematik. Sebagai contohnya: Dalam keluarga anda t...
-
Rumi ke Jawi versi Aplikasi Telefon (Buat masa sekarang, hanya pengguna telefon pintar Android sahaja boleh menggunakanya ). Aplikasi ini...
-
1) Kaedah persenyawaan teknik bantuan Re-produktif (ART). Kaedah ART ialah kaedah yang dilakukan dengan memasukkan sperma/embrio ke dal...
-
Nothing special about me. 😼
-
Pelupusan Sampah dengan cara yang betul amat penting demi kesejahteraan alam ini. Kitar Semula merupakan salah satu cara untuk melupuskan sa...
Post a Comment