PHP 对 JSON 响应不显示任何内容

标签 php mysql json api

我有一个名为 - attend 的数据库,其中有 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 查看器扩展

Screenshot of Output - User Table

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

对于 session 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;
?>

除了更改表名称之外,没有其他任何更改,但这仍然是我得到的 - 空白屏幕

Screenshot of Output - Conference Table

什么,我在这里错过了还是这里出了什么问题?

提前致谢

最佳答案

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

Output of the data in the database

因此,为了使用 json_encode 函数,您必须确保您的数据是 utf8 编码/兼容的。因为如果 json_encode 遇到哪怕一个特殊字符,它就会失败,导致没有输出,因为它无法理解该特殊字符,也无法对其进行解码。

希望这个答案有用。

关于PHP 对 JSON 响应不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50126889/

相关文章:

php - 无法在 html 中显示输出

php - Doctrine DBAL Query Builder 缺少 insert() 方法

php - 为什么查询在 mysql 中有效,prepare 语句有效,但 bind_param 无效?

php - 需要从 json 值中删除反斜杠

java - 未找到 com.sun.jersey.core.util.MultivaluedMapImpl 类和 MIME 媒体类型 application/json

php - Angular Controller 无法与 PHP 和 MySQLi 配合使用

PHP/MYSQL Echo Mysql 数据并带有超链接到下一页?

mysql - 将 MySQL 查询转换为 Hive

java - Jackson继承——使用@JsonProperty解析子类中的嵌套JSON

php - 在 PHP 中检索 Mysql 数据