0

I have this code. But when i use the query and the my national characters ÆØÅ is not beeing displayed and in the json array it just says "NULL".

How can i decode or alter so that i can use these characters? When i set the content charset to iso-8859-1 i get the national characters. But as i have found json encode ONLY uses the utf-8 coding. So how can i use ÆØÅ and get it to work in the json array?

/*
 * Following code will get single utested details
 * A utested is identified by utested id (uid)
 */
header('Content-Type: charset=UTF-8');
// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/dbconnect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["uid"])) {
    $uid = $_GET['uid'];

    // get a utested from utesteds table

    $result = mysql_query("
SELECT p.uid
      , u.name
      , u.description
      , u.url
      , u.picurl
      , u.mapurl
      , p.price
      , ROUND(AVG(r.rating),1) rating
   FROM utested u
   JOIN price p
     ON p.uid = u.uid
   JOIN ( SELECT uid, MAX(pid) latest_price FROM price GROUP BY uid ) px
     ON px.uid = p.uid
    AND px.latest_price = p.pid
   JOIN rating r
     ON r.uid = u.uid
WHERE u.uid=$uid
ORDER BY u.uid DESC LIMIT 1
") or die(mysql_error());

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $utested = array();
$utested["uid"] = $result["uid"];
$utested["name"] = $result["name"];
$utested["description"] = $result["description"];
$utested["url"] = $result["url"];
$utested["picurl"] = $result["picurl"];
$utested["mapurl"] = $result["mapurl"];
$utested["price"] = $result["price"];
$utested["rating"] = $result["rating"];



            // success
            $response["success"] = 1;

            // user node
            $response["utested"] = array();

            array_push($response["utested"], $utested);
//print_r ($response);
            // echoing JSON response
            echo json_encode($response);
        } else {
            // no utested found
            $response["success"] = 0;
            $response["message"] = "No utested found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no utested found
        $response["success"] = 0;
        $response["message"] = "No utested found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

That is my complete PHP file. Please try to adapt so i can test it.

  • http://php.net/utf8_decode ? – Marc B Dec 02 '14 at 21:14
  • 1
    Your code is vulnerable to SQL injections. You should read on [how to prevent them in PHP](http://stackoverflow.com/q/60174/53114). – Gumbo Dec 02 '14 at 21:15
  • Ever thinked of storing to db the html entity and not the actual letter outside utf-8? – Marco Mura Dec 02 '14 at 21:15
  • 1. instead of echo json_encode each time in your else statements why not do it outside once for all of them. It is redundant the way you're doing. 2. use mysqli or PDO. 3. you are vulnerable SQL injections, plus mysql_ functions are no longer maintained. – alda1234 Dec 02 '14 at 21:21
  • Thank you for pointing out vounerabilities. But could you help me with the code. I cant get those characters working – Patrick Leisegang Dec 02 '14 at 23:45

0 Answers0