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?
当我的MySQl数据类型是位(1),但是当用php打印时,json_encode会用unicode写吗? IIS工作正常,但在我的专用服务器中,Apache主机将成为unicode。为什么?
you can see the Locating
, Locating
on Mysql data type is bit, but printed \u0001? why?
你可以看到Locating,Locating on Mysql数据类型是位,但打印\ u0001?为什么?
this is my coding GET method get-googlemarker.php
to get this result
这是我的编码GET方法get-googlemarker.php来获得这个结果
<?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
表示数据是正确的,但问题是当我使用下面的javascript我无法读取定位值,我已经尝试提醒记录,但是是空白。我已经尝试定位类型:字符串,类型:位,类型: bytes,type:int,但不起作用。无法在警报中显示任何内容
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'}
]
});
2 个解决方案
#1
1
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
从PHP 5.4开始,您可以将选项JSON_UNESCAPED_UNICODE与json_encode()一起使用。见http://php.net/manual/en/function.json-encode.php
My guess is that your IIS is returning the unescaped value by default.
我的猜测是你的IIS默认返回未转义的值。
In Javascript you'd usually use parseInt(\u0001)
or something. Since it is extjs, I don't know for that.
在Javascript中,您通常使用parseInt(\ u0001)或其他东西。因为它是extjs,我不知道。
Plan B:
A) Change the database column from bit to integer.
A)将数据库列从位更改为整数。
B) cast the database value in php
B)在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:
C)简单地在json输出中使用str_replace:
$json = json_encode($arr);
$json = str_replace('"\u0001"', '"1"', $json);
$json = str_replace('"\u0000"', '"0"', $json);
#2
0
Use the mysqlnd (native driver) for php. If you're on ubuntu:
使用mysqlnd(本机驱动程序)的PHP。如果你在ubuntu上:
sudo apt-get install php5-mysqlnd
sudo service apache2 restart
The native driver returns integer types appropriately.
本机驱动程序适当地返回整数类型。
this method will only work for PDO (not mysqli).
此方法仅适用于PDO(不是mysqli)。
#1
1
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
从PHP 5.4开始,您可以将选项JSON_UNESCAPED_UNICODE与json_encode()一起使用。见http://php.net/manual/en/function.json-encode.php
My guess is that your IIS is returning the unescaped value by default.
我的猜测是你的IIS默认返回未转义的值。
In Javascript you'd usually use parseInt(\u0001)
or something. Since it is extjs, I don't know for that.
在Javascript中,您通常使用parseInt(\ u0001)或其他东西。因为它是extjs,我不知道。
Plan B:
A) Change the database column from bit to integer.
A)将数据库列从位更改为整数。
B) cast the database value in php
B)在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:
C)简单地在json输出中使用str_replace:
$json = json_encode($arr);
$json = str_replace('"\u0001"', '"1"', $json);
$json = str_replace('"\u0000"', '"0"', $json);
#2
0
Use the mysqlnd (native driver) for php. If you're on ubuntu:
使用mysqlnd(本机驱动程序)的PHP。如果你在ubuntu上:
sudo apt-get install php5-mysqlnd
sudo service apache2 restart
The native driver returns integer types appropriately.
本机驱动程序适当地返回整数类型。
this method will only work for PDO (not mysqli).
此方法仅适用于PDO(不是mysqli)。