mysqli_fetch_assoc (& PDO fetch assoc) storing numbers as strings

后端 未结 1 1612
温柔的废话
温柔的废话 2021-01-23 10:46

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

mysqli_fetch_assoc is (apparently) storing my numbers as strings.

Normally, I could care le

1条回答
  •  爱一瞬间的悲伤
    2021-01-23 11:32

    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.

    Re your comment:

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题