webdeveloper123 Posted January 19 Share Posted January 19 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 Quote Link to comment https://forums.phpfreaks.com/topic/317640-problem-with-echo-json_encode/ Share on other sites More sharing options...
Solution Andou Posted January 19 Solution Share Posted January 19 (edited) 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 January 19 by Andou Quote Link to comment https://forums.phpfreaks.com/topic/317640-problem-with-echo-json_encode/#findComment-1614227 Share on other sites More sharing options...
webdeveloper123 Posted January 19 Author Share Posted January 19 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! 1 Quote Link to comment https://forums.phpfreaks.com/topic/317640-problem-with-echo-json_encode/#findComment-1614228 Share on other sites More sharing options...
webdeveloper123 Posted January 19 Author Share Posted January 19 1 minute ago, webdeveloper123 said: question marks on either side Question mark emojis, not in text Quote Link to comment https://forums.phpfreaks.com/topic/317640-problem-with-echo-json_encode/#findComment-1614229 Share on other sites More sharing options...
Andou Posted January 19 Share Posted January 19 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.) Quote Link to comment https://forums.phpfreaks.com/topic/317640-problem-with-echo-json_encode/#findComment-1614230 Share on other sites More sharing options...
Barand Posted January 19 Share Posted January 19 36 minutes ago, webdeveloper123 said: MAX(m.movie_desc) as movie_desc I don't understand what you are expecting from that line. Quote Link to comment https://forums.phpfreaks.com/topic/317640-problem-with-echo-json_encode/#findComment-1614231 Share on other sites More sharing options...
webdeveloper123 Posted January 19 Author Share Posted January 19 10 minutes ago, Barand said: I don't understand what you are expecting from that line. I was using: GROUP_CONCAT(m.movie_desc) as movie_desc But for 7/8 movies, the movie_desc was rendering multiple times Quote Link to comment https://forums.phpfreaks.com/topic/317640-problem-with-echo-json_encode/#findComment-1614232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.