Asier
Asier

Reputation: 467

php json_encode does not return anything

Im trying to get json_objects from arrays. I get the array that i want to convert but json_encode does not returns anything.

$result = mysqli_query($conn, $query);
$i = 1;
$country = array();
$countries = array();
if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_array($result)){
        echo $row['idPais']." ";
        echo $row['nombre']."<br>";

        $country = array(
                'idPais' => $row['idPais'],
                'nombre' => $row['nombre']
        );
        array_push($countries, $country);
    }
    print_r($countries);

    echo json_encode($countries,JSON_FORCE_OBJECT);
    }else{
    echo "false";
}

print_r($countries) returns this Array:

Array ( [0] => Array ( [idPais] => 7 [nombre] => Espa�a ) 
        [1] => Array ( [idPais] => 8 [nombre] => Francia ) 
        [2] => Array ( [idPais] => 9 [nombre] => Portugal ) 
) 

Upvotes: 2

Views: 972

Answers (1)

Asier
Asier

Reputation: 467

The problem was that i used 'ñ' caracter. For use it with database, i had to put this line in my code:

mysqli_set_charset($conn, "utf8");

Thanks for all.

Upvotes: 2

Related Questions