-1

EDIT WITH SOLUTION!!!! VERY IMPORTANT FOR NOOBS LIKE ME:

The problem was that the data were not in UTF-8 so i include a function that convert these data to UTF-8.

Here the function:

function utf8_string_array_encode(&$array){ $func = function(&$value,&$key){ if(is_string($value)){ $value = utf8_encode($value); } if(is_string($key)){ $key = utf8_encode($key); } if(is_array($value)){ utf8_string_array_encode($value); } }; array_walk($array,$func); return $array; }

Then when i have the new array with string UTF-8, then i use json_encode and the result is:

{"0":"1","CodigoAbogado":"1","1":"24898","NumeroColegiado":"24898","2":"JOAN LLUIS GONZALEZ FERRERI","Nombre":"JOAN LLUIS GONZALEZ FERRERI","3":"","NombreComun":"","4":"","NIF":"","5":"","Direccion":"","6":"juanluis@ferreriabogados.com","Mail":"juanluis@ferreriabogados.com","7":"","CodigoPostal":"","8":"","Poblacion":"","9":"","Provincia":"","10":"Espa\u00f1a","Pais":"Espa\u00f1a","11":"","Notas":"","12":"Administrador","Usuario":"Administrador","13":"1","SerialCertificado":"1","14":0,"CodigoColegioAbogado":0,"15":"","NombreColegioAbogado":"","16":"00","CanalHabitualAbogado":"00","17":"","EmpresaPredeterminadaEosCodigo":"","18":"","EmpresaPredeterminadaEosNombre":"","19":"0000000001","ClienteTurnoOficio":"0000000001","20":"00","ClienteSerieTurnoOficio":"00","21":0,"IPF":0,"22":"","TipoVia":"","23":"","ImportIdentificacion":"","24":"","CertNombre":"","25":" ","Sexo":" ","26":0,"Desc1":0,"27":0,"Desc2":0,"28":0,"Desc3":0,"29":0,"Desc4":0,"30":0,"Desc5":0,"31":0,"Desc6":0,"32":1,"Avis_Guardia":1,"33":0,"Avis_Vista":0,"34":1,"Tiempo_Avis":1,"35":3,"Tiempo_Tipo":3,"36":1,"MostrarInforme":1,"37":"000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000","IdOutLook":"000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000","38":"Nov 10 2011 11:32:54:000AM","UltimaModificacion":"Nov 10 2011 11:32:54:000AM","39":"\u00bd","ESPECIAL":"\u00bd"}]


i have a problem with a little script in php.

the script accesses the database in MS SQL and get data doing a query.

The code is:

<?php

$conn = mssql_connect('PROGRAMACION6', 'Administrador', '');
mssql_select_db('Empresa_00001', $conn);

$queryStr = "Select * from abogados where usuario='Administrador'";

$query= mssql_query($queryStr) or die('MSSQL error: ' . mssql_get_last_message());

$arr = array();

while ($obj = mssql_fetch_array($query)) {
        $arr[] = $obj;
    }

echo '{"Objetos:"'.json_encode($arr).'}';

?>

When i ejecute my script i got a white screen, any data is shown...

But when i put this:

print_r ($arr);

I can see that the array content the follow data:

Array ( [0] => Array ( [0] => 1 [CodigoAbogado] => 1 [1] => 24898 [NumeroColegiado] => 24898 [2] => JOAN LLUIS GONZALEZ FERRERI [Nombre] => JOAN LLUIS GONZALEZ FERRERI [3] => [NombreComun] => [4] => [NIF] => [5] => [Direccion] => [6] => juanluis@ferreriabogados.com [Mail] => juanluis@ferreriabogados.com [7] => [CodigoPostal] => [8] => [Poblacion] => [9] => [Provincia] => [10] => España [Pais] => España [11] => [Notas] => [12] => Administrador [Usuario] => Administrador [13] => 1 [SerialCertificado] => 1 [14] => 0 [CodigoColegioAbogado] => 0 [15] => [NombreColegioAbogado] => [16] => 00 [CanalHabitualAbogado] => 00 [17] => [EmpresaPredeterminadaEosCodigo] => [18] => [EmpresaPredeterminadaEosNombre] => [19] => 0000000001 [ClienteTurnoOficio] => 0000000001 [20] => 00 [ClienteSerieTurnoOficio] => 00 [21] => 0 [IPF] => 0 [22] => [TipoVia] => [23] => [ImportIdentificacion] => [24] => [CertNombre] => [25] => [Sexo] => [26] => 0 [Desc1] => 0 [27] => 0 [Desc2] => 0 [28] => 0 [Desc3] => 0 [29] => 0 [Desc4] => 0 [30] => 0 [Desc5] => 0 [31] => 0 [Desc6] => 0 [32] => 1 [Avis_Guardia] => 1 [33] => 0 [Avis_Vista] => 0 [34] => 1 [Tiempo_Avis] => 1 [35] => 3 [Tiempo_Tipo] => 3 [36] => 1 [MostrarInforme] => 1 [37] => 000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000 [IdOutLook] => 000000002EEFA536C0B8B44B9B44E513587FBA9BA4F22000 [38] => Nov 10 2011 11:32:54:000AM [UltimaModificacion] => Nov 10 2011 11:32:54:000AM [39] => ½ [ESPECIAL] => ½ ) )

How i can convert this array to JSON object? I tested all sample codes that i found on internet but none works...

Thanks a lot very much.

EDIT:

I did that:

switch (json_last_error()) { case JSON_ERROR_NONE: echo ' - JSON_ERROR_NONE'; break; case JSON_ERROR_DEPTH: echo ' - JSON_ERROR_DEPTH'; break; case JSON_ERROR_STATE_MISMATCH: echo ' - JSON_ERROR_STATE_MISMATCH'; break; case JSON_ERROR_CTRL_CHAR: echo ' - JSON_ERROR_CTRL_CHAR'; break; case JSON_ERROR_SYNTAX: echo "\r\n\r\n - SYNTAX ERROR \r\n\r\n"; break; case JSON_ERROR_UTF8: echo ' - JSON_ERROR_UTF8'; break; default: echo ' - Unknown erro'; break; }

And my screeen printed:

JSON_ERROR_UTF8

So how i can resolve that? pffff it is so frustrating...

Eladar
  • 151
  • 1
  • 8

2 Answers2

0

Your data is being fetched as an array of objects, notice the output contains '[0] => stdClass Object'. You are using 'mssql_fetch_object', use 'mssql_fetch_array' instead to get the results as an array.

thanedar
  • 51
  • 6
0

It seems that you have some charset related problem.

json_encode requires that input string is an UTF-8

how your mySQL connection's charset is setted, and what charset / collation is using the table you are fetching data from affects to behaviour of your script.

laxertu
  • 131
  • 3