2

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 ) 
) 
7
  • 1
    You could either tell the Spanish people not to write their language as español or you could connect with UFT-8 to your database. Commented Nov 14, 2016 at 15:36
  • If I apply json_encode to the array directly, it works to me. I have PHP7. Commented Nov 14, 2016 at 15:39
  • 1
    Might help: json_last_error() Commented Nov 14, 2016 at 15:42
  • I have php7 too. I will try without ñ. Commented Nov 14, 2016 at 15:57
  • I tried with the ñ (I'm costarrican) and it worked fine. Commented Nov 14, 2016 at 15:57

1 Answer 1

2

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.

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

Comments

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.