[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

$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>
$(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)
 //using Ajax
   dataType: 'json', // Notice! JSONP <-- P (lowercase)
    // do stuff with json (in this case an array)

b. Using PHP json_decode()

$url = 'http://localhost/path-to-your-file/index.php';
$json = file_get_contents($url);
$datas = json_decode($json);

foreach($datas as $data){
 echo $data->last_name.'

credited to: http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP


Introducing to JSON

There are already lots of tutorials out there on handling JSON with PHP, but most of them don't go much deeper than throwing an array against json_encode and hoping for the best. This article aims to be a solid introduction into JSON and how to handle it correctly in combination with PHP. Also, readers who don't use PHP as their programming language can benefit from the first part that acts as a general overview on JSON.

JSON (JavaScript Object Notation) is a data exchange format that is both lightweight and human-readable (like XML, but without the bunch of markup around your actual payload). Its syntax is a subset of the JavaScript language that was standardized in 1999. If you want to find out more, visit the official website.

The cool thing about JSON is that you can handle it natively in JavaScript, so it acts as the perfect glue between server- and client-side application logic. Also, since the syntactical overhead (compared to XML) is very low, you need to transfer less bytes of ther wire. In modern web stacks, JSON has pretty much replaced XML as the de-factor payload format (aside from the Java world I suppose) and client side application frameworks like backbone.js make heavy use of it inside the model layer.

Before we start handling JSON in PHP, we need to take a short look at the JSON specification to understand how it is implemented and what's possible.

Introducing JSON
Since JSON is a subset of the JavaScript language, it shares all of its language constructs with its parent. In JSON, you can store unordered key/value combinations in objects or use arrays to preserve the order of values. This makes it easy to parse and to read, but also has some limitations. Since JSON only defines a small amount of data types, you can't transmit types like dates natively (you can, but you need to transform them into a string or a unix timestamp as an integer).

So, what datatypes does JSON support? It all boils down to strings, numbers, booleans and null. Of course, you can also supply objects or arrays as values.

Tutorial - MySQL to JSON Data Using PHP

Copy&Paste from: http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP


Popular Posts


Back to Top