0

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

1

1 Answer 1

1

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
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.