Aman Dhiman
Aman Dhiman

Reputation: 303

Ajax post json response issue

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);

}

Upvotes: 2

Views: 95

Answers (5)

sandeep sharma
sandeep sharma

Reputation: 581

@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....";

        }

Upvotes: 1

Manoj Dhiman
Manoj Dhiman

Reputation: 5166

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);
}

Upvotes: 0

Vipul sharma
Vipul sharma

Reputation: 1255

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();

Upvotes: 0

Arun Kumar
Arun Kumar

Reputation: 1667

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);

    }

Upvotes: 0

Dimag Kharab
Dimag Kharab

Reputation: 4519

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);
}

Upvotes: 0

Related Questions