views:

82

answers:

3

I'm trying to store an hexadecimal value (\xC1) in MySql Database. When I print that with PHP, i get: xC1lcool instead of the right word.

INSERT INTO veiculos VALUES (1, 'Ford', 'Ka RoCam Flex', 25850.00, 1998, 1999, 'Vermelho', 15000, '\xC1lcool;Gasolina;GNV', 'Ar-Condicionado;4 portas;Câmbio automático', 'imagem1.jpg;imagem2.jpg;imagem3.jpg;imagem4.jpg;imagem5.jpg;'), (2, 'Ford', 'Ka RoCam Flex', 223850.00, 1999, 2001, 'Prata', 10000, '\xC1lcool;GNV', 'Ar-Condicionado;4 portas;Câmbio automático;', 'imagem1.jpg;imagem2.jpg;imagem3.jpg;imagem4.jpg;imagem5.jpg;'), (3, 'Ford', 'Fiesta', 21380.00, 1998, 2002, 'Preto', 23043, 'Gasolina', '', 'imagem1.jpg;imagem2.jpg;imagem3.jpg;imagem4.jpg;imagem5.jpg;');

How could i do that?

A: 

Use MySQL UNHEX() function with CONCAT(). Like this

insert into tablename values (CONCAT(UNHEX('C1'), 'other-part-of-string'))
shamittomar
A: 

You can store full binary data, such as an image or pdf in a MySQL database. When you insert the value make sure you do a "insert ... values(".mysql_real_escape_string($value).")";. It is recommended that you use the blob datatype for that column, but you can use a varchar if you really want to. After that when you select the data out it will be normal, you don't have to do anything else. In the case of an image, just use a header(content-type: ...) and print out the variable normally and the image will be displayed to the end user.

Rook
+1  A: 

\x is not a valid escape sequence. Valid sequences are detailed at http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

Quoting the manual:

For all other escape sequences, backslash is ignored. That is, the escaped character is interpreted as if it was not escaped. For example, “\x” is just “x”.

You can specify a full string in hexadecimal:

SELECT 0x61626364 -- Prints abcd

If you want to mix both styles, you have to concatenate:

SELECT CONCAT('a', 0x6263, 'd') -- Prints abcd
Álvaro G. Vicario