-1

im working on an app which the users needs to login to. This is coded in Java, the login works without problems. However, i want to receive the "message" from the json and display it. As i said, the code works, but only if the json string "message" dosent contain any special characters like æ, ø, å. I've tried anything, also, i have another file which gets the data from the DB and shows it in a listview, used the exact same code, and this works. The json in the login.php returns NULL, it replaces the whole string and just replaces it with NULL.

My login.php (returning null)

<?php
header('content-type:text/json;charset=utf-8');
$Uname = $_POST["txtUName"];
$Pass = $_POST["txtPass"];
$con=mysqli_connect("localhost","user","pass","db");// Check connection
if (mysqli_connect_errno())
  {
$response["success"] = 0;
 $response["message"] = "Database Error!";
die(json_encode($response));
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
//mysql_query("SET NAMES 'utf8'", $con);
mysql_query("SET NAMES 'utf8'"); 
$result = mysqli_query($con,"SELECT * FROM users where email='$Uname' AND encrypted_password='$Pass'");

while($row = mysqli_fetch_array($result))
  {
$response["success"] = 1;
 $response["message"] = $row['firstname'] . " " . $row['lastname'];
die(json_encode($response));
//header("Location: home.php");
  }

mysqli_close($con);
?> 

My getdata.php (works)

<?php
include "db_config.php";

$cat = $_GET['app_category'];

if($cat == '99'){
        mysql_query("SET NAMES 'utf8'"); 
        $sql=mysql_query("SELECT * FROM `test_fc`");
        while($row=mysql_fetch_assoc($sql)) $output[]=$row;
        print ('{ "app_item":');
        print(json_encode($output));
        print ('}');
        mysql_close();
    } else {
        mysql_query("SET NAMES 'utf8'"); 
        $sql=mysql_query("SELECT * FROM `test_fc` WHERE app_category={$cat}");
        while($row=mysql_fetch_assoc($sql)) $output[]=$row;
        print ('{ "app_item":');
        print(json_encode($output));
        print ('}');
        mysql_close();
    }
?>

My java parser can read the getdata.php without any problem, even with special characters.

I've been stuck on this for the past couple of days. Any help is much appreciated!

EDIT: Getting {"message":null,"success":}

Stian Instebo
  • 653
  • 1
  • 12
  • 31

2 Answers2

0

This has come up before. It's a matter of character encoding and Json expects UTF-8 or you get null. The link explains that you need to encode the string in utf-8 first, it refers to PHP but stackoverflow has adressed this for java as well.

Community
  • 1
  • 1
Useless Intern
  • 1,294
  • 1
  • 10
  • 20
0

The problem is probably this line:

mysql_query("SET NAMES 'utf8'"); 

You are using a mysql_* function while the rest - and your database connection - is using mysqli_*. This will probably lead to a warning in php that gets echoed out and invalidates your json.

You should use either mysqli (preferred) or mysql (deprecated) but not mix them.

jeroen
  • 91,079
  • 21
  • 114
  • 132