json_encode () - Returns the JSON representation of a value
If I do:
$foo = array('a', 'b', 'c', 'd', 'e');
echo json_encode($foo);
I'll get:
["a","b","c","d","e"]
So far so good, I'm getting an array and turning it into a json object.
Now if I try to query and return a multidimensional array to transform into json it does not work.
Example:
include '../db/pdo.php';
$sql = $pdo->prepare('SELECT * FROM CLIENTES WHERE ID > 0');
$sql->execute();
$result = $sql->fetchAll();
echo json_encode($result);
What am I forgetting? Both are getting an array and transforming into JSON.
Array output:
array (size=2)
0 =>
array (size=22)
'CODIGO' => string '1' (length=1)
'NOME' => string 'MARIA DA SILVA' (length=14)
'TIPOPESSOA' => string 'F' (length=1)
'CPF' => string '12345678912' (length=11)
'CGC' => null
'CONTACORRENTE' => null
'REGIAO' => null
'ENDERECO' => null
'NUMERO' => null
'SETOR' => null
'CIDADE' => null
'UF' => null
'CEP' => null
'FONE' => null
'FAXCEL' => null
'ENDENDERECO_1' => null
'NUMERO_1' => null
'SETOR_1' => null
'CIDADE_1' => null
'UF_1' => null
'CEP_1' => null
'FONE_1' => null
1 =>
array (size=22)
'CODIGO' => string '2' (length=1)
'NOME' => string 'SU�LLEM DA SILVA' (length=24)
'TIPOPESSOA' => string 'F' (length=1)
'CPF' => string '12345678912' (length=11)
'CGC' => null
'CONTACORRENTE' => null
'REGIAO' => null
'ENDERECO' => string 'ENDERECO' (length=19)
'NUMERO' => string '401' (length=3)
'SETOR' => string 'SETOR' (length=8)
'CIDADE' => string 'BUENO' (length=13)
'UF' => string 'RO' (length=2)
'CEP' => string '12345678912' (length=8)
'FONE' => string '12345678912' (length=10)
'FAXCEL' => null
'ENDENDERECO_1' => null
'NUMERO_1' => null
'SETOR_1' => null
'CIDADE_1' => null
'UF_1' => null
'CEP_1' => null
'FONE_1' => null
Using json_last_error()
returns 5 = JSON_ERROR_UTF8
.