Ajax发布json响应问题

I have an issue with ajax response. I am using custom query for fetch result from database. Json response always shows null value while query is running successfully. Here is my code:

if(isset($_POST)){

$arr = array();
//$query = mysql_query(" insert into login (user,pass) values ('".$_POST['firstname']."','".$_POST['lastname']."') ") or die('test');

$query = mysql_query(" select * from  login where user = '".$_POST['firstname']."' && pass = '".$_POST['pass']."' ") or die('test');
$rows = mysql_num_rows($query);

    while($fetch = mysql_fetch_array($query))
    {
        if($fetch)
        {
                $_SESSION['ID']= $fetch['id'];
                $arr['id'] = $_SESSION['ID'];

        }
        else
        {
            $arr['failed']= "Login Failed try again....";

        }

}

echo json_encode($arr);

}

Try with the below code, (mysql is deprected), working for me with my test database table (Debug it, var_dump $result, $result->fetch_assoc(), $result->num_rows)

    <?php

    $servername = "localhsot";
    $username = "yourser";
    $password = "passyour";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) 
        die("Connection failed: " . $conn->connect_error);

   if(isset($_POST)){ 

    $arr = array();

    $query = "select * from  login where user = '".$_POST['firstname']."' && pass = '".$_POST['pass']."' ";

    $result = $conn->query($query);

    $rows = $result->num_rows;

        while($fetch = $result->fetch_assoc())
        {

            if($fetch)
            {
                    $_SESSION['ID']= $fetch['id'];
                    $arr['id'] = $_SESSION['ID'];

            }
            else
            {
                $arr['failed']= "Login Failed try again....";

            }

    }

    echo json_encode($arr);
}

try this

 if(isset($_POST)){

    $arr = array();
    //$query = mysql_query(" insert into login (user,pass) values ('".$_POST['firstname']."','".$_POST['lastname']."') ") or die('test');

    $query = mysql_query(" select * from  login where user = '".$_POST['firstname']."' && pass = '".$_POST['pass']."' ") or die('test');
    $rows = mysql_num_rows($query);
        if($rows>0)
        {
            while($fetch = mysql_fetch_array($query))
            {
                 $_SESSION['ID']= $fetch['id'];
                 $arr['id'] = $_SESSION['ID'];
            }
        }else
            {
                $arr['failed']= "Login Failed try again....";

            }

    echo json_encode($arr);

    }

First of all start session before playing with it on the top of page

session_start();

check your database connectivity.

Use print_r($arr) for testing your array();

first off all you are using session variable . to use session variable you need to initialize it by session_start()

you are using key in the array in this way it will return the last inserted record in the array . try this code

<?php

    $servername = "localhsot";
    $username = "yourser";
    $password = "passyour";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) 
        die("Connection failed: " . $conn->connect_error);

   if(isset($_POST)){ 

    $arr = array();

    $query = "select * from  login where user = '".$_POST['firstname']."' && pass = '".$_POST['pass']."' ";

    $result = $conn->query($query);

    $rows = $result->num_rows;

        while($fetch = $result->fetch_assoc())
        {

            if($fetch)
            {
                    // if wants to use session then start session 
                    session_start();  // else it will return null
                    $_SESSION['ID']= $fetch['id'];   // i don't know why this ??

                   // $arr['id'] = $_SESSION['ID'];   comment this 
                    $arr[] = array('msg'=>'succsee','ID'=>$fetch['id']);

            }
            else
            {
                $arr[]= array('msg'=>'fail','ID'=>null);

            }

    }

    echo json_encode($arr);
}

@Amandhiman i did not get what is the use of if statement with in the while

if($fetch)
    {
            $_SESSION['ID']= $fetch['id'];
            $arr['id'] = $_SESSION['ID'];

    }

the mention code definitely works for you

    if($rows>0)
    {
        while($fetch = mysql_fetch_array($query))
        {
             $_SESSION['ID']= $fetch['id'];
             $arr['id'] = $_SESSION['ID'];
        }
    }else
        {
            $arr['failed']= "Login Failed try again....";

        }