Write result of a search in a json_encode

1

I'm trying to write a result of a query to a json_encode and soon after writing to a field in my BD , but I'm getting an error message and could not solve the problem.

What I'm 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 DB is in the format MediumText .

    
asked by anonymous 26.07.2018 / 19:12

1 answer

1

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

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

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

    
27.07.2018 / 16:38