【发布时间】:2011-06-22 13:17:06
【问题描述】:
我在 PHP 中遇到问题。我正在尝试从 Android 传递用户名/密码并通过 PHP 检查 MySQL 中的值。使用json_decode 和json_encode 时,json_decode 有效,但json_encode 无效。但是当我删除 json_decode 时,json_encode 可以工作,但我希望它们都可以在我的程序中工作。
这是我的代码:
$a = $_POST['userpwd_value']; //Accesing the value from Android.
$b = json_decode($a); //Decoding android value using JSON.
$username = $b->{'username'}; //Assigning username from android to a variable.
$password = $b->{'password'}; //Assigning password from android to a variable.
echo $username.$password;
$check = mysql_query("select username,password from user where id=1");
$row = mysql_fetch_assoc($check);
//if($row['username']==$username && $row['password']==$password)
$output[]=$row;
//else
//$output[]=array("value"=>"false");
print(json_encode($output));
问题出在哪里?
【问题讨论】:
-
你试过用 print_r(json_encode($output)) 看看输出是什么
-
您是否确定 $row 包含您希望它包含的内容?也许这就是 json_encode 没有返回预期值的原因。
-
从代码中删除
echo $username.$password怎么样? -
@Adnan 它在 PHP 中产生结果,但在 android 中没有
-
@Crashspeeder 是的,我已经检查了 $row 值 @Gumbo json_decode 会产生输出,但 json_encode 不会 @Michael J.V 结果不会改变