-
-
Notifications
You must be signed in to change notification settings - Fork 63
Bit(1) values being fetch as empty string #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Okay, after some thinking I figured the strings weren't empty and I was missing an empty space that could mean only one thing, they were byte strings. For an instant I forgot about that wonderful PHP thing (I don't like that). Well, so I looked for where the byte values are converted to PHP values: Then I made some tests and my thinking was right, I could successfully convert the BIT values to int from the byte string: Now I know this is not a bug, it is by design. But I don't understand why, since PDO would simply convert the BIT to an integer I was expecting the same behavior. I think BIT values in MySQL are closer to integers. Although they’re stored as binary data, they represent numeric bit-fields and are used in bitwise operations, which aligns them more with integers than with strings. Is there a specific reason why you've implemented this, this way? Would you be willing to accept a change on that behavior? |
This is motivated by amphp#137 (comment) where > BIT values in MySQL are closer to integers. Although they’re stored as binary data, they represent numeric bit-fields and are used in bitwise operations, which aligns them more with integers than with strings.
This is motivated by: > BIT values in MySQL are closer to integers. Although they’re stored as binary data, they represent numeric bit-fields and are used in bitwise operations, which aligns them more with integers than with strings. (amphp#137 (comment))
As to why it's that way: https://dev.mysql.com/doc/dev/mysql-server/8.4.3/page_protocol_binary_resultset.html#sect_protocol_binary_resultset_row_value_string According to the protocol it's sent as a length encoded string. I see that php-src has special handling for BIT. We probably should replicate it for amphp/mysql. But simply parsing as tinyint is not correct. |
I.e. what PHP does is taking the binary string and converting it to int, if the number fits into int, otherwise into string. This is not really trivial, like a straightforward test shows:
hexdec converts to float. base_convert does also (and then back to string, wtf). Using |
Actually we can probably combine str_pad with unpack(). |
@bwoebi should that be something to consider updating in the PR I have open? |
Also, in case this doesn't go forward, is there a way of extending the driver so I can customize the BIT value parsing for my project? |
For some reason all the columns I have in my database that are of type Bit(1) (I didn't check for other Bit sizes) are being fetch as empty strings, I don't know exactly what to look for in order to further investigate this, sorry about the lack of information but I'm open to collaborate to get more data if you give me some light.
In a script as simple as this I can reproduce the issue I'm facing:
The columns of type smallint works just as expected. Before, with PDO, all BIT columns would return as integers so this is currently a big problem for me.
Just to be clear I'm trying to use AMP MySQL as a PDO replacement for Eloquent (based on https://github.com/xpader/amphp-eloquent-mysql) and its been super positive so far, the only issue I have now is with this BIT column.
The text was updated successfully, but these errors were encountered: