how to get mysql data as json format in php


 
 
 
 
 
 
 
 

how to get mysql data as json format in php

<?php
    
    
first create connectivity to your database
afterwards follow the code instruction and hints
 
$connect=mysqli_connect(“localhost”,”root”,”root”,”sales”);
    
   $result = mysqli_query($connect,”SELECT * from user_info”) or die(‘Could not query’);
if(mysqli_num_rows($result)){
    echo ‘{“testData”:[‘;
    $first = true;
    $row=mysqli_fetch_assoc($result);
    while($row=mysqli_fetch_row($result)){
        //  cast results to specific data types
        if($first) {
            $first = false;
        } else {
            echo ‘,’;
        }
        echo json_encode($row);
    }
    echo ‘]}’;
} else {
    echo ‘[]’;
}
?>