mysqli_fetch_assoc(& PDO fetch assoc)将数字存储为字符串 [英] mysqli_fetch_assoc (& PDO fetch assoc) storing numbers as strings

查看:138
本文介绍了mysqli_fetch_assoc(& PDO fetch assoc)将数字存储为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和往常一样,我有责任到处寻找溶胶,但无济于事.

as usual, did my duty to look everywhere for the sol but to no avail.

mysqli_fetch_assoc(显然)将我的数字存储为字符串.

mysqli_fetch_assoc is (apparently) storing my numbers as strings.

通常,我不太在乎,但是我的网站几乎是100%的ajax,并且它会移动大量数据,因此所有这些json都开始累加.

Normally, I could care less, but my site is nearly 100% ajax, and it moves around a lot of data, so all of those json "'s start to add up.

如果我只获取一个列值,则可以进行intval,但是我想使用相关的列名来获取整行.

If I'm just grabbing one column value, I can intval, but I want to grab whole rows with associated column names.

有没有办法使mysqli_fetch_assoc(和PHP PDO的assoc)将数字存储为数字?

Is there a way to get mysqli_fetch_assoc (and PHP PDO's fetch assoc) to store numbers as numbers?

非常感谢!

推荐答案

MySQL可以存储64位数字和无符号数字,PHP的int类型默认都不支持这两种数字.如果您尝试从溢出PHP中int变量的数据库中获取数字数据,则会丢失信息.这就是PHP数据库客户端将数字作为字符串提取的原因.

MySQL can store 64-bit numbers and unsigned numbers, neither of which are supported by default in PHP's int type. If you try to fetch numeric data from the database that overflows the int variables in PHP, you'll lose information. That's why PHP database clients fetch numbers as strings.

发表您的评论

是的,如果您担心JSON的大小,请在PHP应用程序中转换数据以获得更紧凑的表示形式.

Yes, if you're concerned about the size of JSON, convert the data in your PHP app for more compact representation.

尽管您说过要在JSON中传输列名,但字符串格式的整数和二进制整数之间的区别似乎并不是瓶颈.

Though since you said you're transferring column names as part of your JSON, it seems like the difference between string-formatted ints and binary ints is not your bottleneck anyway.

我的意思是,将string-int转换为二进制int可使位减少约50%.但是,无论如何,如果您将列名作为字符串使用,则只缩小了JSON的一小部分.列仍然是字符串.

I mean, converting a string-int to a binary int reduces the bits by about 50%. But if you have column names as strings anyway, you're only shrinking a small portion of the JSON. The columns are still strings.

这篇关于mysqli_fetch_assoc(& PDO fetch assoc)将数字存储为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆