How to convert MYSQL data to JSON using PHP

By | January 28, 2017

Today i will  Convert my data from MySQL to JSON and it should be possible effortlessly with the assistance of PHP. Generally individuals incline toward JSON over XML as JSON is anything but difficult to parse when contrasted with XML. It is choosed over XML on the grounds that it is plain content and in comprehensible configuration, light weight and as key esteem combine. So lets check how to change over MySQL to JSON utilizing PHP.

PHP json_encode() function is used for encoding JSON in PHP. This function returns the JSON representation of a value on success or FALSE on failure.

The following example shows how the PHP objects can be converted into JSON

<?php
   class obj{
      public $product = "";
      public $decription  = "";
      public $price = "";
   }
 
   $p = new obj();
   $p-product = "Watch";
   $p->decription = "The new Apple Watch is the ultimate device for your healthy life. Choose from a range of models ";
   $p->price  = "500$";
 
   echo json_encode($p);
?> 

While executing, this will produce the following result

{"product":"Watch","decription":"The new Apple Watch is the ultimate device for your healthy life. Choose from a range of models","price":"500$"}

 

Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *