gpt4 book ai didi

php - Ajax post json响应问题

转载 作者:行者123 更新时间:2023-12-01 04:44:47 26 4
gpt4 key购买 nike

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

}

最佳答案

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

}

关于php - Ajax post json响应问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com