This site had helped me a lot in the past, but now I am lost. Thanks in advance for your guidance.
I have a MySQL table that contains a Binary value, like the example below. I cannot change the table.
CREATE TABLE `test` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`nid` binary(16) NOT NULL,
`test` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`))
This an example value of nid: ÞFÈ>ZPÎ×jRZ{æ×
(not all showing, but all 16 are there)
Now I want to create a SQL Query to look for the id of the row where this value is true.
SELECT id FROM test WHERE nid = 'ÞFÈ>ZPÎ×jRZ{æ×';
... does not work. Any idea?
SOLUTION Obtaining the nid in HEX format did the trick. It results in DE46C83E5A50CED70E6A525A7BE6D709 and when I use this in the query like this ...
SELECT id FROM test WHERE HEX(nid) = 'DE46C83E5A50CED70E6A525A7BE6D709';
I am getting the right result.
The MySQL BINARY function is used for converting a value to a binary string. The BINARY function can also be implemented using CAST function as CAST(value AS BINARY). The BINARY function accepts one parameter which is the value to be converted and returns a binary string.
MySQL is a Relational Database Management System (RDBMS) that uses Structured Query Language (SQL). It is also free and open source. The combination of PHP and MySQL gives unmet options to create just about any kind of website – from a small contact form to large corporate portal.
The algorithm is a binary search (there are optimizations and improvements, but below is the general theory behind it). Say you want to search for number 5000, There are two ways. scan the entire list, in which case you will have to check 10 numbers (count from start until you reach 5000).
The syntax for declaring Binary variable is binary(n) , where n defines the size in bytes. Note that size is in bytes and not number of characters. For Example, when we declare as binary(10) , The column will occupy 10 bytes of storage. The value of n can be from 1 to 8000 bytes.
Note: This addresses binary data, but not encrypted data. See this answer for searching on encrypted data.
Try adding X
, x
or 0x
in front of binary data used for search:
SELECT id FROM test WHERE pid = '0xÞFÈ>ZPÎ×jRZ{æ×';
EDIT: try also this:
SELECT id FROM test WHERE BINARY pid = 'ÞFÈ>ZPÎ×jRZ{æ×';
OR
SELECT id FROM test WHERE HEX(pid) = BIN2HEX('0xÞFÈ>ZPÎ×jRZ{æ×');
as supposed here: How to select with a binary field ? (php,mysql)
IF NOTHING FROM ABOVE WORKS: Try obtaining the pid
in HEX
format, like
SELECT id, HEX(pid) pid, test FROM test
and then when searching try only:
SELECT id, test FROM test WHERE HEX(pid) = '{$my_pid}'
But I'm not sure how do You obtain the pid
data to PHP or even whether You pass the binary data into Your select - where
query... Just guessing due to the php
tag...
try:
X'' --Hex Content
mysql> SELECT x'4D7953514C';
-> 'MySQL'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With