When trying to insert values into a BIT(64) column using MySQLi prepared statements, I am receiving the error (on execute):
Data too long for column 'bits64' at row 1
Here's my code:
if ($Segments[0] == 'yes') {$bitmask = '1';}else{$bitmask = '0';}
if ($Segments[1] == 'yes') {$bitmask = $bitmask.'1';}else{$bitmask = $bitmask.'0';}
...
if ($Segments[63] == 'yes') {$bitmask = $bitmask.'1';}else{$bitmask = $bitmask.'0';}
$Upload = $conn->prepare("INSERT INTO `testtable` (`bits64`) VALUES (?)");
$Upload->bind_param("s", $bitmask);
$Upload->execute();
The code provides a 64 character string. If I use PHPMyAdmin to insert the same output, it works successfully and looks like this:
INSERT INTO `testtable` (`bits64`) VALUES (b'0000000000000000000000000000000000000000000000000000000000000000');
INSERT INTO `testtable` (`bits64`) VALUES (b'1111111111111111111111111111111111111111111111111111111111111111');
The only thing that has shown any "success" (*not really) is changing the bind type to integer. But it appears that it is treated like an integer that should be converted to binary, since it gives the same error with the bit flags set to 1
.
Other things I have tried include formatting $bitmask
as:
$bitmask = "b'".$bitmask."'";
$bitmask = "0b".$bitmask;
// And others
As far as I am aware, the only bind_param types are integer, string, double, and blob. I assume it's sending the values like '101010'
(code sample), 101010
(as integer), 'b'101010''
, and '0b101010'
, respectfully; however, what is needed is b'101010'
.
For speed and security reasons (of other fields), I have to use prepared statements.
I initially used BIGINT for this, but it appeared that rounding errors were occurring when most of the flags were set.
I think I have MySQL strict mode disabled.
My fallback is either a lot of TINYINT columns or CHAR(64), but I would prefer more efficient storage.
There are some related questions on this website, but most relate to single bit fields, where an integer 0 or 1 is more easily correlated to the single bit. I should be most appreciative of any advice or solutions.
EDIT
I realized that I did not have SQL-Mode completely blank. Once SQL Mode is an empty string, there are no errors but I get 0111111111111111111111111111111111111111111111111111111111111111
no matter what I try.
EDIT 2
Inserting via phpMyAdmin with all 1 flags set also gives 0111111111111111111111111111111111111111111111111111111111111111
, so there must be other problems with the way I have this column configured. I am using Ubuntu 16.04, Server version: 5.7.19, PHP version: 7.0.22.