Ajax发布json响应问题

时间:2015-07-08 09:44:34

标签: php jquery ajax json

我遇到了ajax响应问题。我正在使用自定义查询从数据库中获取结果。查询成功运行时,Json响应始终显示空值。这是我的代码:

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

}

5 个答案:

答案 0 :(得分:1)

@Amandhiman我没有得到if语句在while中的用法

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

    }

提及代码绝对适合您

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

        }

答案 1 :(得分:0)

尝试使用以下代码( mysql被删除),使用我的测试数据库表(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);
}

答案 2 :(得分:0)

试试这个

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

    }

答案 3 :(得分:0)

首先开始会话,然后在页面顶部播放

  

在session_start();

检查您的数据库连接。

使用 print_r($ arr)来测试你的数组();

答案 4 :(得分:0)

首先关闭你正在使用的会话变量。要使用会话变量,需要通过session_start()

对其进行初始化

您正在以这种方式使用数组中的键,它将返回数组中最后插入的记录。试试这段代码

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