PHP无法显示JSON响应

时间:2018-05-02 03:51:27

标签: php mysql json api

我有一个名字的DB - 出勤有2个表

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;

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', 'hariharansmm@gmail.com', 224596236, 'Electronic Partners', 'FD E7 A0 26 39 77', 0, 0),
(18, 67890, 'Ulaganathan Natarajan', 'n.ulaganathan1990@gmail.com', 2102920557, 'Electronic Partners', 'F2 35 1D E6 8C 63', 0, 0),
(19, 13579, 'Richard', 'richard@elex.co.nz', 123456789, 'Electronic Partners', 'EE CA 10 52 99 80', 0, 0),
(20, 24680, 'Shankar Subramanian', 'shankars@elex.co.nz', 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;

我尝试为这两个表创建API JSON,这些是PHP文件

对于用户表

<?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;
?>

显示的内容 - 注意:我已经为Chrome安装了JSON Viewer Extension

Screenshot of Output - User Table

问题来了,当我为conference_schedule表尝试相同的操作时,我什么都没得到 - 没有错误,空白页

对于会议桌

<?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;
?>

除了更改表名之外,没有其他任何内容被更改,但这仍然是我得到的 - 一个空白的scrren

Screenshot of Output - Conference Table

我在这里错过了什么,或者这里有什么问题?

先谢谢

1 个答案:

答案 0 :(得分:1)

请查看您的表格定义。您已使用&#34; latin1&#34;创建了表格。字符集。这可能包含特殊字符。 json_encode函数仅处理utf8字符集。打印数据库记录时,您会发现某些记录包含表格数据中未经过utf8编码/兼容的特殊字符,请参见下面的屏幕截图:

Output of the data in the database

因此,为了使用json_encode函数,您必须确保您的数据是utf8编码/兼容的。因为如果json_encode甚至遇到一个特殊字符,它就会失败而导致没有输出,因为它无法理解特殊字符并且无法解码它。

希望这个答案很有用。