Monday, 2 May 2016

Convert Data from Mysql to JSON Formate using PHP

In PHP, Changing Data from MySQL to JSON type is one of the important a piece of work to be done in Web Development. JSON has obtained demand before the many years and it can be used over xml as data exchange format in web applications. By Using json format has its own benefits a like, it is being light weight, it has ability to store big type of data structures in simple text format and it has can be easily human readable. JSON is a data exchange format among web or mobile applications and it can be smoothly export data into plain text which can be human readable format and it can smoothly performance with any types of languages. Now I have show how to convert mysql data to json in php.


 -- Table structure for table `tbl_employee`  
 CREATE TABLE IF NOT EXISTS `tbl_employee` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `name` varchar(50) NOT NULL,  
  `gender` varchar(10) NOT NULL,  
  `designation` varchar(30) NOT NULL,  
  PRIMARY KEY (`id`)  
 -- Dumping data for table `tbl_employee`  
 INSERT INTO `tbl_employee` (`id`, `name`, `gender`, `designation`) VALUES  
 (1, 'Michael Bruce', 'Male', 'System Architect'),  
 (2, 'Jennifer Winters', 'Female', 'Senior Programmer'),  
 (3, 'Donna Fox', 'Female', 'Office Manager'),  
 (4, 'Howard Hatfield', 'Male', 'Customer Support');  


 <!DOCTYPE html>  
           <title>Webslesson Tutorial | Convert Data from Mysql to JSON Format using PHP</title>  
           $connect = mysqli_connect("localhost", "root", "", "testing");  
           $sql = "SELECT * FROM tbl_employee";  
           $result = mysqli_query($connect, $sql);  
           $json_array = array();  
           while($row = mysqli_fetch_assoc($result))  
                $json_array[] = $row;  
           /*echo '<pre>';  
           echo '</pre>';*/  
           echo json_encode($json_array);  

1 comment: