title: Getting null value in my json response.
body: <p>I have the following registration php script. I want to get back a json response as 
      {"result":"success","message":"123"}</p>
  
  <p>where 123 is the id of the registered user. I want this id so at a later point the user can post data to other tables. </p>
  
  <p>However I get this.</p>
  
  <pre><code>{"result":"fail","message":null}
  </code></pre>
  
  <p>This is my script.</p>
  
  <pre><code>&lt;?php
    session_start();
    require "init.php";
    header('Content-type: application/json');
    $id = $_POST['id'];
    $email = $_POST['email'];
    $user_name = $_POST['user_name'];
  
    $user_pass = $_POST['user_pass'];
    $passwordEncrypted = sha1($user_pass);  
  
    $confirmPass = $_POST['confirm_pass'];
    $confPasswordEncrypted = sha1($confirmPass);  
  
    $msg = "Congratulations. You are now registered to the most amazing app   
    ever!";            
  
          if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
  
              $don = array('result' =&gt;"fail","message"=&gt;"Please enter a valid email");
  
          }    
  
  if($email &amp;&amp; $user_name &amp;&amp; $user_pass &amp;&amp; $confirmPass &amp;&amp; filter_var($email, FILTER_VALIDATE_EMAIL)){
  
  
      $sql_query = "select * from user_info WHERE email  ='".mysqli_real_escape_string($con, $email)."' or user_name 
      ='".mysqli_real_escape_string($con, $user_name)."'";
  
      $result = mysqli_query($con, $sql_query);   
  
      $results = mysqli_num_rows($result);
  
      if ($results){
          $don = array('result' =&gt;"fail","message"=&gt;"Email or username exists.");
  
      }else{
          //This is where I am trying to get the id
          while($row = mysqli_fetch_array($result)) {             
              $posts['id'] = $row['id'];
  
  
          }   
  
          $sql_query = "insert into user_info values('$id','$email','$user_name','$passwordEncrypted','$confPasswordEncrypted');";
  
          if(mysqli_query($con,$sql_query)){
              $_SESSION['id'] = mysqli_insert_id($con);
              //And this is the json response I was talking about
              $don = array('result' =&gt;"success","message"=&gt;$posts['id']);
              mail($email,"Well done. You are registered to my sample app!",$msg);
  
          }
      }
  }else if(!$email){
  
  
          $don = array('result' =&gt;"fail","message"=&gt;"Please enter a valid email");               
  
  
      }else if(!$user_name){
  
          $don = array('result' =&gt;"fail","message"=&gt;"Please enter your username");
  
      }else if(!$user_pass){
  
          $don = array('result' =&gt;"fail","message"=&gt;"Please enter a password");
  
      }else if(!confirmPass){
  
          $don = array('result' =&gt;"fail","message"=&gt;"Please confirm your    
          password");
  
      }     
  
    echo json_encode($don);
  
   ?&gt;
  </code></pre>