json_encode not returning anything

I know this is old, but I didn't found the explanation of this error, in my case, the problem was to keept the values on the DB with accent mark (Ej: cafetería). the var_dump($emparray ) certanly show information, but the echo json_ecode($emparray ) shows nothing. The solution?

This is my DB conection:

$connection = mysqli_connect('ip,'user','pass','dbname') or die("Error " . mysqli_error($connection));

Only need to add the correct charset:

mysqli_set_charset( $connection, 'utf8');

Expetcs this work for others.


try this

while ( $row = $result->fetch_assoc() ){
    $emparray[] = json_encode($row);
}
echo json_encode( $emparray );

or

while($row =mysqli_fetch_assoc($result))
{
   $emparray[] = json_encode($row);
}
echo json_encode($emparray);

or

$emparray = $result->fetch_all( MYSQLI_ASSOC );
echo json_encode( $emparray );

instead of

 while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

Add this line and it will work fine:

mysqli_set_charset($conn, 'utf8');

Tags:

Mysql

Php

Json