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.

Database


 --  
 -- 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`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;  
 --  
 -- 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');  


mysql_to_json.php


 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Convert Data from Mysql to JSON Format using PHP</title>  
      </head>  
      <body>  
           <?php   
           $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>';  
           print_r(json_encode($json_array));  
           echo '</pre>';*/  
           echo json_encode($json_array);  
           ?>  
      </body>  
 </html>  

5 comments:



  1. Hi everybody, I hope you are doing good. Dear admin I had scroll down your site and found it really useful for web development tools. Since I am a developer, I most often refer people to use best website in the town for web development. Your efforts are really appreciable. We also provide web development tool. Here is the link of our site jsononline


    ReplyDelete
  2. please see the code for front end
    but it gives me error
    if i copy data and paste them it works
    but i want to load them from source can you pls help

    $('#employee_data').editable({
    container: 'body',
    selector: 'td.gender',
    url: "update.php",
    title: 'Status',
    type: "POST",
    dataType: 'json',
    source: "mysql_to_json.php",
    validate: function(value){
    if($.trim(value) == '')
    {
    return 'This field is required';
    }
    }
    });




    });

    ReplyDelete