0

I have a Phpscript that should extract latin1_swedish_ci encoded Data from Mysql database, the extracted data is then encoded with json_encode method, everything works fine unless the extracted rows contains special characters like é è ....., in that case i get no results, and and the HTML status code is 200

That is my function

public function categorie()
    {   
        // Cross validation if the request method is GET else it will return "Not Acceptable" status
        if($this->get_request_method() != "GET"){
            $this->response('',406);
        }
        $sql = mysql_query("SELECT * FROM categorie", $this->db);
        if(mysql_num_rows($sql) > 0)
        {
            $result = array();
            while($rlt = mysql_fetch_array($sql,MYSQL_ASSOC)){
                $result[] = $rlt;
            }
            // If success everythig is good send header as "OK" and return list of users in JSON format
            $this->response($this->json($result), 200);
        }
        $this->response('',204);    // If no records "No Content" status
    }

    private function json($data){
        if(is_array($data)){
            return json_encode($data);
        }
    }       
Hossam Oukli
  • 1,296
  • 3
  • 19
  • 42
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – HamZa Aug 18 '13 at 16:35
  • Status code 200 is "ok" – user4035 Aug 18 '13 at 16:37
  • After reading your question several times. I realised that you just threw at us the situation and some code that isn't even "visible" to us. For example What does `json()` do ? It's a function you created and we could not know what's happening. Anyways, how are you certain it's UTF-8 encoded ? I think you should: **1-** check that the field (in your table) is indeed UTF-8 **2-** Try setting utf-8: `mysql_set_charset('utf8');` **3-** Maybe you should try `utf8_encode()` ? – HamZa Aug 18 '13 at 16:48
  • Sorry about that I've just updated the question, – Hossam Oukli Aug 18 '13 at 16:51
  • Ok I've just checked again, i found at that the table i was trying to extract data from was actually `latina_swedish_ci`, it seems i need to update my question – Hossam Oukli Aug 18 '13 at 16:57
  • What exactly happens, what results are you getting? – Pekka Aug 18 '13 at 16:58
  • Empty result and as i said that specific table was actually latin swedish not utf8,you can try it yourself : http://hossamoukli.alwaysdata.net/rest/api.php?rquest=categorie the same thing here but no special characters http://hossamoukli.alwaysdata.net/rest/api.php?rquest=users – Hossam Oukli Aug 18 '13 at 17:00
  • Do you call `mysql_set_charset` to set the character encoding for the database connection? `json_encode` accepts only strings that are encoded in UTF-8. – Joni Aug 19 '13 at 09:09
  • The canonical resource for these issues is [UTF-8 all the way through](http://stackoverflow.com/q/279170) – Pekka Aug 19 '13 at 12:48

0 Answers0