Posted in API Flutter PHP & MySQL

PHP – Valid way to Generate Nested or Complex JSON data for Flutter

PHP – Valid way to Generate Nested or Complex JSON data for Flutter Posted on May 9, 20203 Comments
I'm Hari Prasad Chaudhary from Nepal, developer of the finest educational website/app "MeroSpark" and the finest eCommerce system "PasalaY". Here I share the code snippet which I learn during development.

I used to build chrome extension and worked with JSON and Javascript, at that time I have certain ways to generate Nested JSON with PHP but, when I come to Flutter App Development, that causes an error while parsing. After that, I get another way; certainly, a valid way to generate the JSON. I have shown that method in the example below.

Database Table:

Student List Table
Student List Table

PHP Code:

<?php 
  $db = "test_db";
  $host = "localhost";
  $db_user = 'root';
  $db_password = 'root';
  //MySql server and database info

  $link = mysqli_connect($host, $db_user, $db_password, $db);
  //connecting to database

  $json["error"] = false; //boolean output
  $json["errmsg"] = "";
  $json["data"] = array();
  //json output array

  $sql = "SELECT * FROM student_list ORDER BY full_name ASC";
  $res = mysqli_query($link, $sql);
  $numrows = mysqli_num_rows($res);
  if($numrows > 0){
      
     //check if there is any data
      while($array = mysqli_fetch_assoc($res)){
           array_push($json["data"], $array);
      }

      /* Do not push data like below,
      it will cause error on flutter json prasing. 
      user array_push() function.  

      $datalist = array();
      $x = 1;
      while($array = mysqli_fetch_assoc($res)){
           $datalist[$x] = $array;
           $x++;
      }
      $json["data"] = $datalist;

      */
  }else{
      $json["error"] = true;
      $json["errmsg"] = "No any data to show.";
  }
  
  mysqli_close($link);

  header('Content-Type: application/json');
  // tell browser that its a json data
  echo json_encode($json);

?>

JSON Output:

{
   "error":false,
   "errmsg":"",
   "data":[
      {
         "student_id":"4",
         "full_name":"Anil Chaudhary",
         "address":"Kailali, Nepal",
         "class":"Nine",
         "roll_no":"9",
         "dob":"1997-02-28"
      },
      {
         "student_id":"5",
         "full_name":"Arjun Chaudhary",
         "address":"Chaumala, Nepal",
         "class":"Ten",
         "roll_no":"10",
         "dob":"1995-04-10"
      },
      {
         "student_id":"2",
         "full_name":"John Karki",
         "address":"Pokhara, Nepal",
         "class":"Nine",
         "roll_no":"3",
         "dob":"1996-02-21"
      },
      {
         "student_id":"6",
         "full_name":"Karan Chaudhary",
         "address":"Dang, Nepal",
         "class":"Eight",
         "roll_no":"33",
         "dob":"1993-04-03"
      },
      {
         "student_id":"1",
         "full_name":"Krishna Chaudhary",
         "address":"Kathmandu, Nepal",
         "class":"Ten",
         "roll_no":"7",
         "dob":"1994-06-25"
      },
      {
         "student_id":"3",
         "full_name":"Rabi Shahi",
         "address":"Dhangadhi, Nepal",
         "class":"Ten",
         "roll_no":"5",
         "dob":"1994-02-25"
      }
   ]
}
I'm Hari Prasad Chaudhary from Nepal, developer of the finest educational website/app "MeroSpark" and the finest eCommerce system "PasalaY". Here I share the code snippet which I learn during development.

3 comments

  1. I get error this error bro

    Error : mysqli_num_rows() expects parameter 1 to be mysqli_result at line 18

    1. Hello Bipin Brother,
      Once try adding this line and see the output.

      $res = mysqli_query($link, $sql);
      echo mysqli_error($link);

      It will show what is wrong in your code. Be sure that database connection is correct and SQL query is valid.

Leave a Reply

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