emma
emma

Reputation: 759

PDO fetchAll() - json_encode doesn't work when using JOIN

I'm using PDO to fetch all rows from a table:

$pdo = new PDO('mysql:host=127.0.0.1;dbname=learning', 'root', '');

$query = $pdo->preapre("
    SELECT * 
    FROM people 
    INNER JOIN city
    ON 
    people.id = city.person_id
");
$query->execute();

$people = $query->fetchAll(PDO::FETCH_ASSOC);    

Now at this point if i print_r this $people variable i'm getting a multidimensional array that contains all those rows from that table:

Array
(
    [0]=>Array
        (
        [id] => 1
        [name] => Emma
        [city] => New York
        )
    [1]=>Array
        (
        [id] => 2
        [name] => John
        [city] => Los Angeles
        )
    //and so on
)

But if i want to json_encode that $people variable and then echo it i'm getting a blank screen:

$j = json_encode($people);
echo $j;

I think i'm doing something pretty wrong but i don't understand what, can somebody please help me understand and fix this?

Thank you! :D

UPDATE

On using var_dump:

var_dump($j);

I'm getting:

bool(false)

P.S I should mention that i changed the query to an INNER JOIN - without INNER the json_encode worked

Upvotes: 0

Views: 86

Answers (1)

Emanuel Ones
Emanuel Ones

Reputation: 289

You have to set up your charset to UTF8 in your pdo connection:

$pdo = new PDO('mysql:host=127.0.0.1;dbname=learning;charset=UTF8', 'root', '');
//your code

Upvotes: 1

Related Questions