PHP JSON Encoding | Lessoncup Programming Blog

Tuesday, 17 December 2013

PHP JSON Encoding

Hi, today i am going to tell you about how to convert an object to JSON using PHP and it is also use to create a webSerive and send json data to other platforms's like php to flash and others, it is very easy to write the script to convert Mysql records to JSON array, let's have a look on script and demo.

PHP JSON Encoding, is a simple trick just we need to write the query and have to use json_encode() function


here connect this file to database and write the query to convert the records into JSON Array, before looping the records create an Array named $obj_user and loop the records using while() loop and inside the loop just assign the column name or names to created Array $obj_user[]=$full['username']; and close the loop at end of the loop use json_encode() to convert data to an JSON Array, just keep the created array inside the json_encode($obj_user) function, now see the output you can see the JSON Array

now you can send this link to others and use this link as a webservice and decode the JSON data and use it to your applications easily.
<div class="lessoncup">


$user=mysql_query("select * from users");


echo "Total&nbsp;".count($obj_user)." Array Elements Found<br/><br/>";

echo json_encode($obj_user);



Live Demo


  1. Simple script and easy to implement any other application.

  2. Sorry, this is a poor tutorial that does a whole lot of things wrong.

    The mysql_* functions are deprecated and will be removed in future versions of PHP. Use the PDO API instead. Why do self-proclaimed programmers keep writing articles using a library that is over 15 years old and has been replaced by newer, safer and more reliable standard libraries almost ten years ago?

    Your variable naming is poor. For instance $obj_user sounds like a user object, when it's actually an array of usernames.

    $full is also a poor name, because English does not have any such noun. A better name would be $row, because it's an associative array containing the columns of a single row from the database result set.

    A nonbreaking space is also unneccessary in this situation.

    And your article lacks context as webservices do not echo JSON datastructures inside an HTML document.

    Here is a more correct version of the code.

    $database_connection = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');

    $result_set = $database_connection->query('SELECT username FROM users');

    $usernames = [];
    while($username = $result_set->fetchColumn()) {
    $usernames[] = $username;

    echo json_encode($usernames);

    The entire article can be boiled down to "PHP 5.2 and newer has a global function called json_encode() that can turn any PHP object into a string of JSON"

    1. Dear Niels Buus.. there is many ways to do programming and i agree with some points which you write here but one thing you remember this is just a tutorial and this is not project to put it in live application.. and if you talking about feature .. slowly every one will adopt the new things ..and thank you ..


Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA

Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons