0

I'm trying to display JSON in my PHP file. The problem is that it doesn't show anything, like it was empty.

Code

<?php
include ('conexion.php');
//$id = $_GET['id'];
$sql = "SELECT tbl_usuarios.idUsuario, tbl_usuarios.nombrec, tbl_actividades.idUsuario, tbl_actividades.fec_actividad, tbl_actividades.nombre FROM tbl_actividades INNER JOIN tbl_usuarios ON tbl_usuarios.idUsuario = tbl_actividades.idUsuario WHERE tbl_usuarios.idUsuario = 1";

if($conexion){

    if(!$result = mysqli_query($conexion,$sql)) die();

        while($data = mysqli_fetch_array($result)){
            $arreglo[] = $data;
        }
        echo json_encode($arreglo);
    }else{
        die ("error");
    }

?>

It only shows when I use print_r($arreglo).

3
  • 2
    What is the output of print_r($arreglo)? Commented Mar 2, 2018 at 21:37
  • What does the browsers "View page source" feature show? Commented Mar 2, 2018 at 21:40
  • On which line in that code did you try print_r($arreglo)? It seems possible that the query failed and the script died before it could do anything with $arreglo. Commented Mar 2, 2018 at 21:44

1 Answer 1

1

Some of your database objects are in Spanish, it is most probable that your result array, $arreglo is not encoded in utf-8. Because to Spanish uses special characters like 'ñ', it is most probable your default database encoding is not utf-8. When such characters are encountered, the function json_encode fails and shows nothing. Because such function only works with utf-8 results.

One way to deal with this is to pass your array through this function so it cast everything into utf-8. add the following function to your PHP.

 function cast_data_types ($value) {
      if (is_array($value)) {
        $value = array_map('cast_data_types',$value);
        return $value;
      }
      if (is_numeric($value)) {
        if(strpos('.', $value)===false) return (float)$value;
        return (int) $value;
      }
      return utf8_encode((string)$value);
    }

You can also apply JSON_UNESCAPED_UNICODE to properly show special characters back.

so instead of echo json_encode($arreglo);

you should input echo json_encode(cast_data_types($arreglo),JSON_UNESCAPED_UNICODE);

If the above does not work you can always use json_last_error() to check what error was encountered during json_encode or json_decode operations.

http://php.net/manual/es/function.json-last-error.php

Sign up to request clarification or add additional context in comments.

3 Comments

Nice answer. It's a bit speculative, but the assumption is reasonable. Might want to mention that it's possible to check for errors encountered by json_encode to verify that that's truly what the problem is here.
@Don'tPanic thanks for the edit, sorry for my english it is not my first language. I am going to edit in what you say about checking for errors.
Your English seems fine to me! I really only edited because of the typo in "function". Normally I wouldn't have bothered to edit just to capitalize a couple of things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.