Save a search result to a json_encode

Asked

Viewed 45 times

1

I’m trying to record a result of a query in a json_encode and right after recording in a field in my BD, but I am getting an error message and could not solve the problem.

What am I doing:

$IdUsuario = $rowData['IdUsuario'];

// RECUPERANDO DADOS
$queryResult = array();
$this->GetConnection()->ExecQueryToArray('SELECT * FROM cadUsuario WHERE cadUsuario.IdUsuario = ' . $IdUsuario, $queryResult);

$string = json_encode($queryResult);

// INSERINDO DADOS DO ITEM LOG
$IdUsuario = $this->GetEnvVar('CURRENT_USER_ID');
// INSERINDO DADOS DA TABELA DE LogItens
$this->GetConnection()->ExecSQL('INSERT INTO cadLogItem (IdLog, Operacao, DataHoraOcorrencia, IdUsuario, Instancia) VALUES ('.$IdLog.', "AT", CURRENT_TIMESTAMP, '.$IdUsuario.', '.$string.' )');

And the error message is this:

Cannot execute SQL statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[{"0":"26","IdUsuario":"26","1":"[email protected]","Email":"carolmerigue' at line 1

The field in the comic book is in the format MediumText.

  • If it’s a string, no quotation marks in SQL?

  • Hello @Andersoncarloswoss, quote -> '.$string.'

  • These are just to concatenate. Inside SQL there will be no quotes. Just debug your code to understand the problem.

  • of an echo in $string. And show the result. I wonder what the problem is.

1 answer

1


I had a problem with the same purpose, save json in a database, so I found the solution to my problem, simple and easy:

$string = json_encode(addslashes($queryResult));

Because of the quotes and/or apostrophes, the query always gave problem, so I used addslashes and solved the problem, I hope it helps this tip.

  • 1

    Perfect @Wees Smith, thanks for the excellent tip.

Browser other questions tagged

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