Jump to content

Problem with echo json_encode


Go to solution Solved by Andou,

Recommended Posts

Hi Guys,

Been at this for 2 days now. I've got a script here where I am trying to encode the movies array to JSON and pass it to a Javascript file. The thing is if I do:

        echo json_encode(array_column($movies, 'title'));

It works, but only returns the title attribute (which is what array_column is supposed to do) But I want to send the whole thing in and display all the data. So I am trying:

echo json_encode($movies);

But it won't send anything.

Here is a sample from print_r of the array:

Array
(
    [0] => Array
        (
            [title] => Aquaman And The Lost Kingdom
            [screens] => 1,4,7,5
            [date] => Wednesday, 17th Jan
            [image] => aquaman.jpg
            [genres] => Action
            [running_time] => 2 hours 4 minutes
            [start_times] => 10:00,13:00,16:00,19:00
            [movie_desc] => When an ancient power is unleashed, Aquaman must 
forge an uneasy alliance with an unlikely ally to protect Atlantis, and the world, from irreversible devastation.
        )

    [1] => Array
        (
            [title] => Ferrari
            [screens] => 2,6,7,5
            [date] => Wednesday, 17th Jan
            [image] => ferrari.jpg
            [genres] => Drama
            [running_time] => 2 hours 10 minutes
            [start_times] => 12:30,15:30,18:30,21:30
            [movie_desc] => It is the summer of 1957. Behind the spectacle of Formula 1, ex-racer Enzo Ferrari is in crisis.
        )

Also when I use array_column statement, and go to the page  - say for example: getMovies.php?day=Wednesday

It will show this (correctly):

["Aquaman And The Lost Kingdom","Ferrari","Mean Girls","Next Goal Wins","Night Swim","One Life","Priscilla","Wonka"]

but when I use:

echo json_encode($movies);

I get a blank page.

Here is the code:

    if (isset($_GET['day'])) {
        $day = $_GET['day'];

        $query = "SELECT 
    m.title,
    GROUP_CONCAT(s.screen_num) as screens,
    date_format(sg.screen_on, '%W, %D %b') as date,
    m.image,
    g.genres,
    CONCAT(m.running_time DIV 60, ' hours ', m.running_time % 60, ' minutes') as running_time,
    GROUP_CONCAT(TIME_FORMAT(sg.screen_at, '%H:%i')) as start_times,
    MAX(m.movie_desc) as movie_desc  
FROM screening sg
JOIN screen s ON sg.screen_id = s.screen_id
JOIN movie m ON sg.movie_id = m.movie_id
JOIN (
    SELECT m.movie_id, GROUP_CONCAT(g.description) as genres
    FROM movie m
    JOIN genre g ON g.genre_id = m.genre_id
    GROUP BY m.movie_id
) g ON g.movie_id = m.movie_id
WHERE dayname(screen_on) = :day
GROUP BY sg.screen_on, m.title
ORDER BY sg.screen_on;";


        $statement = $conn->prepare($query);
        $statement->bindParam(':day', $day);
        $statement->execute();

        $movies = $statement->fetchAll((PDO::FETCH_ASSOC));  

    //echo json_encode($movies);
        
        echo json_encode(array_column($movies, 'title'));
    }

Thanks

Link to comment
Share on other sites

  • Solution

A couple of things.

First, put error_reporting(E_ALL); and ini_set('display_errors' 1); at the top of the file so we can see the errors.

Second, I googled it and apparently calling "json_last_error()" after json_encode() may fix the issue if it gives you the error code.

Try it, and if it still doesn't work, I'll see what else can be done.

Edited by Andou
Link to comment
Share on other sites

Hey Andou,

Thanks for that link. I read it and someone was having the same problem as me. When the guy said he changed the encoding type, I instantly knew what was wrong with it. In movie_desc attribute I had a word in single quotes : ‘Nicky’ - When I was reading the print_r from the array is was rendering that word with question marks on either side. I didn't think much of it - until I read that thread! 

  • Like 1
Link to comment
Share on other sites

Glad to hear it! Sometimes, Google-Fu and putting stuff in quotes is the best way to solve problems.

(By the way, if it's solved, there's a button you can click somewhere on the thread to mark it as solved.)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.