0

I have data stored in mysql database, the data is encoded in ascii because of some special characters, i am trying to fetch data for iphone application, the json format is correct but due to special characters it returned me null 42756666616c6f205068696c6c79e28099732077696e67732c20636865657365737465616b7320616e64206d6f7265 this is stored in mysql table while actually it is Buffalo Philly’s wings, cheesesteaks and more i tried with html_entity_decode, html2text but none help me, please help on this i am really stucked.

here is the code how i fetch data from db and print it in json format:

$sql = "SELECT  p.product_id,p.image,p.model,d.name,d.product_id,d.language_id  FROM product AS p ,product_description AS d

        WHERE 
        d.product_id = p.product_id
        AND
        d.language_id = 1

        ORDER BY p.date_added


        limit 10
    ";

$rs = mysql_query($sql);
$data = array();
$products = '{"products":{';
$num = mysql_num_rows($rs);

while($d = mysql_fetch_array($rs)){
$image = new SimpleImage();
$image->load($path.$d['image']);
$image->resize(55,55);
$img = strtotime("now").$count;
$image->save("images/".$img.'.jpg');
$pimage = "$img_url/$img.jpg";

$name = html_entity_decode($d['name'],ENT_QUOTES, 'UTF-8');


$highlight = html_entity_decode($d['model'],ENT_QUOTES, 'UTF-8');


if($count < $num){
    $products .= '"'.$d["product_id"].'":[
                                            {
                                                "id":"'.$d["product_id"].'",
                                                "name":"'.$name.'",
                                                "image_url":"'.$pimage.'",
                                                "highlight":"'.$highlight.'"
                                            }
                                        ],';
    }else{
    $products .= '"'.$d["product_id"].'":[
                                            {
                                                "id":"'.$d["product_id"].'",
                                                "name":"'.$d["name"].'",
                                                "image_url":"'.$pimage.'",
                                                "highlight":"'.$highlight.'"
                                            }
                                        ]';
}

}
$products .='}}';

print($products);

Object-c code is:

NSData *data = [NSData dataWithContentsOfURL:soURL];
    NSError *error;
    productsRaw = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    products = [productsRaw objectForKey:@"products"];
    productKeys = [products allKeys];
MIrfan
  • 123
  • 8

1 Answers1

0

That string seems to be hex encoded. You can use the pack function, like below:

pack('H*', $data);

as explained in this SO page, or something close to it (the 'h*' packing parameter may work).

Beware that the resulting string will be locale encoded, so depending on which locales were used in the original application, you may have to do some locale conversion to get a properly encoded string. For that purpose, I suggest the use of the iconv function, but at any rate you have to know the originally used locale.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52