Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query with binary data (PHP and MySQL)

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.

like image 552
user2007877 Avatar asked Jan 24 '13 16:01

user2007877


People also ask

How do I get binary in MySQL?

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.

Can we use PHP and MySQL together?

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.

Does MySQL use binary search?

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).

How do you write binary in SQL?

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.


2 Answers

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...

like image 189
shadyyx Avatar answered Sep 28 '22 19:09

shadyyx


try:

X''   --Hex Content

mysql> SELECT x'4D7953514C';
    -> 'MySQL'
like image 4
jixiang Avatar answered Sep 28 '22 18:09

jixiang