MySql - bigints、php 和自动字符串/int 转换翻转 [英] MySql - bigints, php and auto string/int casting flip-flopping
问题描述
昨天我问了一个关于 bigints 的问题,得到了友好的回答.但是,我一直在观察一些奇怪的行为,并想了解发生了什么.
I asked a question about bigints yesterday which was kindly answered. However, I have been observing some weird behaviour and would like to understand what is going on.
在我的 php 中,我有一个数组,我将它发送回使用它的 javascript web 客户端程序.
In my php I have an array which I send back to a javascript web client program which uses it.
在php中
sendBack = null;
sendBack[0]['TimeStamp'] = $Time; // A bigint got from a mysql table milliseconds from 1970
sendBack[0]['Text'] = $Message; // A varchar message got back from mysql
// I am guessing at this point you have worked out this is a text-chatroom thing going on
sendBack[1]['TimeStamp'] = 0; // A zero indicates an admin issue - note its an int but a small one
sendBack[1]['Text'] = $MessageAdmin;
// And I pack it up to send back
echo json_encode($sendBack);
在 js 中,我将其解压缩以用于:
In the js I unpack it to use with:
var json = eval('(' + data + ')');
问题是,js 中的 0 索引时间戳被视为字符串,而索引 1 时间戳被视为整数.
The problem is, the 0 index TimeStamp in the js is being treated as a string but the index 1 Timestamp is being treated as an int.
从教育的角度来看,有人知道发生了什么吗?
From an educational point of view does anyone know what is going on?
推荐答案
我相信从 PHP 数据库返回的值总是字符串.为了转换您的零索引时间戳,您需要执行以下操作:
I believe values returned from a database with PHP are always strings. In order to convert your zero-index timestamp you'd need to do something like:
sendBack[0]['TimeStamp'] = parseInt($Time, 10);
将其转换为整数值(基数为 10).
which will convert it to an integer value (base-10).
显然,1-index 时间戳被直接设置为零,因此它以 int 形式返回.
Obviously the 1-index timestamp is being set as zero directly hence the reason it's returning as an int.
这篇关于MySql - bigints、php 和自动字符串/int 转换翻转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!