Search in a JSON encoding field

Asked

Viewed 661 times

1

There is a field in the table where the content before being written is encoded in JSON.

The content is saved as follows:

[["nome_completo","Ana Concei\u00e7\u00e3o Dias"],["idade","27 anos"],["cidade","Lisboa"]]

In Phpmyadmin, select below returns no results:

SELECT * 
FROM funcionarios
WHERE custom LIKE '%Ana Concei\u00e7\u00e3o Dias%'

How to make the above query return the respective record?

  • What database are you using? I ran a test here on Mysql and SQL Server 2008 and both returned the right result.

  • @Andréribeiro I’m using Mysql.

  • Add to your question the PHP code you are using. It might be something there.

  • @Andréribeiro I’m using phpmyadmin I removed the PHP tag to avoid confusion.

  • Try WHERE custom LIKE '%Ana Conceição Dias%'

  • @bfavaretto tried that too and it didn’t work.

  • Try then escaping each bar: \

  • 1

    @bfavaretto succeeded, but using 4 bars. " Ana Concei u00e7 u00e3o Dias".

Show 3 more comments

2 answers

1

My code below is not a complete solution, copy/Paste will not work. It is intended to push you in the right direction. I suppose my code may have some errors, although I’m not seeing it. If anyone finds the error, please, DEIXE UM COMENTÁRIO.

First I wanted to know why you converted the results to Json before saving to the bank. You already have an array, you don’t need a JSON string in your case. You need to convert back with this:

$yourArray = json_decode($json)

Now you can enter the data into the table. I don’t know how your Insert is , but if I look at your code, I think your sql would look something like this:

$sql = 'INSERT INTO tabela(id, nome_completo, idade, cidade) VALUES (:id, :nome_completo, :idade, :cidade)';

So your code would look something like this:

$stmt = $db->prepare( $sql );
$sql = 'INSERT INTO tabela(id, nome_completo, idade, cidade) VALUES (:id, :nome_completo, :idade, :cidade)';
$stmt->bindParam(':id', $yourArray ['id']);
$stmt->bindParam(':nome_completo', $yourArray['nome_completo']);
...
$stmt = $dbh->prepare( $sql );
$stmt->execute();

This is the correct way to store your "Json" data, so you can make select smoothly.

  • Rafael, I appreciate the help but the problem is not how to write the data but how to search in a field that has been saved with JSON encoding. I didn’t say I built the system like this, I’m just trying in Mysql to do a query in a table that has the field encoded. The system is not mine but if it were would do as you did, separate fields and use Prepared statements.

  • Of course my answer is not about a data recording problem, but about A PROBLEM THAT WAS CREATED BY RECORDING IN THE ASKED WAY. So I pointed out a "suitable" solution. Good luck!

1


I found the solution here: http://dev.mysql.com/doc/refman/5.6/en/string-comparison-functions.html#operator_like

Since Mysql uses C escape syntax in strings ( for example, "n" to represent a new line ), it is necessary to double any "\".

For example, to search " n" it is necessary to specify " n". To search " ", specify it as " " ; this is because the backslashes are deleted once by the parser and again when pattern matching is done, leaving a single backslash to be searched.

The query was as follows:

SELECT *  
FROM funcionarios
WHERE custom LIKE '%Ana Concei\\\\u00e7\\\\u00e3o Dias%'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.