Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how come MySQL bit data type php print unicode?

When my MySQl Data Type is bit(1) ,but when printed with php json_encode will write with unicode? IIS are working fine,
but in my dedicated server Apache hosting will become unicode. WHY? enter image description here

you can see the Locating, Locating on Mysql data type is bit, but printed \u0001? why?

this is my coding GET method get-googlemarker.php to get this result

<?php
mysql_connect("localhost", "root", "123456") or die("Could not connect");
mysql_select_db("db_gps") or die("Could not select database");

$parent_id = $_GET['mainid'];

$query = "SELECT        *
FROM          tbl_locate AS a
INNER JOIN     
(
    SELECT    MainID, Max(DateTime) AS DateTime
    FROM      tbl_locate
    GROUP BY  MainID
) AS b
ON            a.MainID = b.MainID
AND           a.DateTime = b.DateTime
LEFT JOIN     
(
    SELECT b.PicData
     , b.PicUploadedDateTime
     , b.MainID
  FROM (SELECT    MainID,Max(PicUploadedDateTime) as PicUploadedDateTime
        FROM      tbl_picture
        group by MainID
       ) l
  JOIN tbl_picture b
    ON b.MainID = l.MainID AND b.PicUploadedDateTime = l.PicUploadedDateTime
) AS c
ON            a.MainID = c.MainID";

$rs = mysql_query($query);

$arr = array();
while($obj = mysql_fetch_object($rs)) {
 $arr[] = $obj;
}

echo json_encode($arr);


?>

UPDATED

the representation data are correct, but the problem is when i m using below javascript i can't read the Locating value, i have tried alert the record, but is blank.i have tried Locating with type:string , type:bit , type:bytes , type:int ,
but does not work. can't show anything in alert

Ext.define('GoogleMarkerModel', {
        extend: 'Ext.data.Model',
        fields: [
        {name: 'ID',    type: 'int'},
        {name: 'Locating',    type: 'int'},
        {name: 'MainPower',    type: 'int'},
        {name: 'Acc',    type: 'int'},
        {name: 'PowerOff',    type: 'int'},
        {name: 'Alarm',    type: 'int'},
        {name: 'Speed',    type: 'int'},
        {name: 'Direction',    type: 'int'},
        {name: 'Latitude',    type: 'float'},
        {name: 'Longitude',    type: 'float'},
        {name: 'DateTime',    type: 'datetime'},
        {name: 'MainID',    type: 'int'},
        {name: 'IOState',    type: 'int'},
        {name: 'OilState',    type: 'int'},
        {name: 'PicUploadedDateTime',    type: 'datetime'},
        {name: 'PicData',    type: 'str'}
        ]
    });
like image 426
John Walker Avatar asked Nov 13 '22 00:11

John Walker


1 Answers

The presentation of the variable is still correct.

Since PHP 5.4, you can use the option JSON_UNESCAPED_UNICODE with json_encode(). See http://php.net/manual/en/function.json-encode.php

My guess is that your IIS is returning the unescaped value by default.

In Javascript you'd usually use parseInt(\u0001) or something. Since it is extjs, I don't know for that.

Plan B:

A) Change the database column from bit to integer.

B) cast the database value in php

while($obj = mysql_fetch_object($rs)) {
  $obj->Locating = (int)$obj-Locating;
  // or try with (string) if (int) fails
  $arr[] = $obj;
}

C) simply str_replace in the json output:

$json = json_encode($arr);
$json = str_replace('"\u0001"', '"1"', $json);
$json = str_replace('"\u0000"', '"0"', $json);
like image 177
Daniel W. Avatar answered Nov 14 '22 21:11

Daniel W.