Hariharan Thiagarajan
Hariharan Thiagarajan

Reputation: 65

PHP Displaying Nothing for JSON Response

I have a DB by the name - attendance which has 2 tables

CREATE TABLE `conference_schedule` (
  `id` int(5) NOT NULL,
  `session` varchar(1024) DEFAULT NULL,
  `start` timestamp NULL DEFAULT NULL,
  `end` timestamp NULL DEFAULT NULL,
  `actstart` timestamp NULL DEFAULT NULL,
  `actend` timestamp NULL DEFAULT NULL,
  `room` varchar(1024) DEFAULT NULL,
  `processed` int(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `conference_schedule` (`id`, `session`, `start`, `end`, `actstart`, `actend`, `room`, `processed`) VALUES
(1, 'Innovation - From Strategy Through Tactical To Operational', '2018-05-19 21:30:00', '2018-05-20 00:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Cliftons Melbourne', 0),
(2, 'Story Telling for Leaders - How To Influence, Engage and Inspire', '2018-05-19 21:30:00', '2018-05-20 05:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Cliftons Melbourne', 0),
(3, 'Speak And Be Heard – Strategies And Skills To Be Heard When It\'s Difficult To Be Heard', '2018-05-19 21:30:00', '2018-05-20 05:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Cliftons Melbourne', 0),
(4, 'Agile & Lean - What You Really Need To Know And Why', '2018-05-19 21:30:00', '2018-05-20 05:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Cliftons Melbourne', 0),
(5, 'Overcoming The Global Epidemic Of Dysfunctional Organisations And Disengaged Employees', '2018-05-19 21:30:00', '2018-05-20 00:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Cliftons Melbourne', 0),
(6, 'Fast - Track PMI - ACP Agile Certified Practitioner', '2018-05-19 21:30:00', '2018-05-20 05:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Cliftons Melbourne', 0),
(7, 'Think With Your Hands: Performance Management - A Master Class Facilitated With Lego Serious Play', '2018-05-20 01:30:00', '2018-05-20 05:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Cliftons Melbourne', 0),
(8, 'Opening Ceremony', '2018-05-20 20:30:00', '2018-05-20 20:45:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(9, 'Opening Speech', '2018-05-20 20:45:00', '2018-05-20 21:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(10, 'Scrappy Design Thinking: Simple Rules, Practical Tools', '2018-05-20 21:00:00', '2018-05-20 21:45:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(11, 'Managing Diversity From The Inside And Out', '2018-05-20 21:45:00', '2018-05-20 21:15:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(12, 'If You Met A Goldfish - Effective Communication For A Diverse Workplace', '2018-05-20 22:45:00', '2018-05-20 23:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(13, 'How PMO Maturity Assessment Can Create A Road Map For Your Future PMO', '2018-05-20 22:45:00', '2018-05-20 23:35:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(14, 'Cyber Security - Risk and Growth #TrueStory', '2018-05-20 22:45:00', '2018-05-20 23:35:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(15, 'The Industry 4.0 Art Of War - Superior Performance Leadership 4.0 Through Diversity & Wellbeing', '2018-05-20 23:35:00', '2018-05-21 00:15:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(16, 'PMI Australia Panel Discussion: Gender Diversity - Fact or Fiction', '2018-05-21 01:15:00', '2018-05-21 02:05:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(17, 'Diversity @ Work: A Session Facilitated With Lego Serious Play', '2018-05-21 01:15:00', '2018-05-21 02:50:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(18, 'Complexity In ICT Enabled Change - Practical Appraches', '2018-05-21 01:15:00', '2018-05-21 02:50:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(19, 'The Value Of Project Management - PMI\'s 2017 Pulse Of The Profession', '2018-05-21 03:15:00', '2018-05-21 04:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(20, 'The True Story Of A Life Lost And Found', '2018-05-21 04:00:00', '2018-05-21 05:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(21, 'Opening Speech', '2018-05-21 20:45:00', '2018-05-21 21:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(22, 'Storytelling: A Project Management Superpower', '2018-05-21 21:00:00', '2018-05-21 21:45:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(23, 'Let Yourself Be Heard And Make A Difference', '2018-05-21 22:45:00', '2018-05-21 23:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(24, 'Program Management: A New Australian Renaissance - And A Critical Element Of Our Economy And Security', '2018-05-21 22:45:00', '2018-05-21 23:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(25, 'Build Your Project Management Career Brand By Improving Linkedin Performance', '2018-05-21 22:45:00', '2018-05-21 23:30:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(26, 'Methodology Battles Is Making Project Delivery Complex - But It Shouldn\'t', '2018-05-21 23:35:00', '2018-05-22 00:20:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(27, 'Executive Leadership: Mindset Is The Biggest Issue! Why Is This And How Can We Tackle It?', '2018-05-21 23:35:00', '2018-05-22 00:15:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(28, 'Agile And The Future Of Project Management', '2018-05-22 01:15:00', '2018-05-22 02:05:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(29, 'Australia\'s Newest Radio Telescope - Project Theory And Practical Delivery', '2018-05-22 01:15:00', '2018-05-22 02:05:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(30, 'The Value Of Being Human In A Future Of Artifical Intelligence And Automation', '2018-05-22 02:05:00', '2018-05-22 02:50:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(31, 'Will Robots Steal My Job?', '2018-05-22 02:05:00', '2018-05-22 02:50:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0),
(32, 'The Great Debate', '2018-05-22 03:15:00', '2018-05-22 03:45:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'Crown Melbourne', 0);

ALTER TABLE `conference_schedule`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `conference_schedule`
  MODIFY `id` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=33;
COMMIT;

and

CREATE TABLE `users` (
  `id` int(5) NOT NULL,
  `pmiid` int(10) DEFAULT NULL,
  `user_name` varchar(512) DEFAULT NULL,
  `user_email` varchar(512) DEFAULT NULL,
  `user_num` int(10) DEFAULT NULL,
  `user_org` varchar(512) DEFAULT NULL,
  `user_mac` varchar(128) DEFAULT NULL,
  `user_flag` int(3) DEFAULT NULL,
  `user_percent` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `users` (`id`, `pmiid`, `user_name`, `user_email`, `user_num`, `user_org`, `user_mac`, `user_flag`, `user_percent`) VALUES
(17, 12345, 'Hariharan Thiagarajan', '[email protected]', 224596236, 'Electronic Partners', 'FD E7 A0 26 39 77', 0, 0),
(18, 67890, 'Ulaganathan Natarajan', '[email protected]', 2102920557, 'Electronic Partners', 'F2 35 1D E6 8C 63', 0, 0),
(19, 13579, 'Richard', '[email protected]', 123456789, 'Electronic Partners', 'EE CA 10 52 99 80', 0, 0),
(20, 24680, 'Shankar Subramanian', '[email protected]', 21489993, 'Electronic Partners', 'D4 13 A2 0C B5 90', 0, 0);

ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `users`
  MODIFY `id` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=21;
COMMIT;

I tried creating API JSON for both the tables and these were the PHP Files

For User Table

<?php
    include("database/db_conection.php");
    $query="SELECT * FROM `users`";
    $run=mysqli_query($dbcon,$query);
    while($row=mysqli_fetch_assoc($run))
    {
        $user[] = $row;
    }
    $myJSON=json_encode($user);
    echo $myJSON;
?>

Which displayed - Note: I have installed JSON Viewer Extension for Chrome

Screenshot of Output - User Table

The problem comes, when I try the same for conference_schedule table, I get nothing - No error, blank page

For Conference Table

<?php
    include("database/db_conection.php");
    $query="SELECT * FROM `conference_schedule`";
    $run=mysqli_query($dbcon,$query);
    while($row=mysqli_fetch_assoc($run))
    {
        $user[] = $row;
    }
    $myJSON=json_encode($user);
    echo $myJSON;
?>

Except changing the Table name, nothing else is changed but still this is what I get - A blank scrren

Screenshot of Output - Conference Table

What, am I missing here or what is wrong here?

Thanks in Advance

Upvotes: 0

Views: 74

Answers (1)

Kishen Nagaraju
Kishen Nagaraju

Reputation: 2180

Please look at your table definitions. You have created the tables with "latin1" charset. This may contain special characters. The json_encode function only deals with utf8 charset. When printing the database records, you can find that some of the records contains special characters that is not utf8 encoded/compatible in your table data, please see the screenshot below:

Output of the data in the database

So, in order to work with json_encode function, you have to make sure that your data is utf8 encoded/compatible. Because if json_encode experiences even one special character, it'll fail resulting in no output as it cannot understand the special character and will not be able to decode that.

Hope this answer is useful.

Upvotes: 1

Related Questions