I am getting a problem in PHP. I am trying to pass the username/password from Android and checking the value in MySQL through PHP. While using json_decode and json_encode, json_decode works but json_encode does not work. But when I remove the json_decode, json_encode works, but I want both of them to work in my program.
Here is my code:
$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));
Where is the problem?
json_encodefails if the variable content is not correctly UTF-8 sequenced. If your database uses another charset, the variables contain special characters, then you should get an error there. (raise theerror_reportinglevel or checkjson_last_errorto find out)Another problem with your specific code is that you first output something else:
This will invalidate the JSON output as a whole. If you have leading garbage, your browser will not decode the returned variables correctly. Also don’t forget to send the appropriate header with your result using
header("Content-Type: application/json");