0

I have this code:

    $json = array();
$hoy = date("Y-m-d");
$consulta = "SELECT e.*,s.sal_nombre, concat(eve_titulo, ' - ',sal_nombre) as title FROM evento e, sala s where s.sal_id=e.sal_id ORDER BY id";

// conexión bbdd
try {
$bdd = new PDO('mysql:host=localhost;dbname=prueba2', 'root', '');
} catch(Exception $e) {
exit('Unable to connect to database.');
}

//ejecutamos consulta
$resultado = $bdd->query($consulta) or die(print_r($bdd->errorInfo()));

// enviamos el resultado codificado en json a la página que lo llama
echo json_encode($resultado->fetchAll(PDO::FETCH_ASSOC));

$result = $resultado->rowCount();
print_r($result);

when printing result, it prints 4 that is the correct result but the json_encode returns nothing and if i do print_r($resultado->fetchAll());

it returns an empty array

Mariana Hernandez
  • 1,919
  • 4
  • 19
  • 29

1 Answers1

2

The issue happens because the returned data isn't encoded in utf-8 as it is required by json_encode.

To fix it the charset=utf8 attribute must be added to the PDO's DSN.

zerkms
  • 249,484
  • 69
  • 436
  • 539