1

I am using Android to create a app. Where user answer are store on mySQL on server via PHP code.

function fetch_all_complaints_by_packet($packet) {
    global $dBConnection;

    $query = "SELECT * FROM ".DB_PREFIX ."complaint ORDER BY id DESC LIMIT 0 , 10";
    $result = mysql_query($query,$dBConnection) or die('Errant query:  '.$query);

    $complaints = array();

    if(mysql_num_rows($result)) {
        $i = 0;

        while($complaint = mysql_fetch_assoc($result)) {
            $complaints[$i] = $complaint;
            echo ($complaint['details'].'<br>');
            $i++;
        }
    }

    //echo json_encode($complaints);
}

The following line

echo json_encode();

Cause error it show details as NULL, but

echo json_encode($complaint['details'].'<br>');

Print the correct data. What the problem is?

Max
  • 15,693
  • 14
  • 81
  • 131
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
  • I do not understand at all what the problem is. Can you clarify? – Pekka Oct 05 '11 at 16:27
  • 'special characters' getting mangled usually means a character set mis-match. Unless you mean SQL metacharacters, as in your question title. If that's the case, then you need to read up about SQL injection: http://bobby-tables.com – Marc B Oct 05 '11 at 16:28
  • possible duplicate of [Best way to stop SQL Injection in PHP](http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php) – hakre Oct 05 '11 at 16:31
  • I fixed up the formatting a bit, but I still dont have any clue what your problem is. Did you try http://php.net/json_last_error and see if there is any error message? – Max Oct 05 '11 at 16:58

1 Answers1

3

I've had something similar to this maybe check on your side if the problem is the same:

When you have an invalid UTF-8 string (invalid utf-8 characters) and try to JSON_Encode that data, it will return NULL.

It took me hours to find that in my code because after all it is AJAX call so you can't really see the errors coming out.

My suggestion is to turn error logging on and display all errors, you will probably see an error about UTF-8 encoding that failed. If not, try to utf8_decode your data and see if it shoots out the error i told you about.

Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71