使用 JSON 解析 MySQL 数据

Parsing MySQL Data using JSON

我正在使用 MAMP 本地主机服务器并尝试从下面的 table 获取数据

我没有收到任何语法错误或堆栈错误,但是当浏览器页面为空白时,是的,display_errors 和 php.in 文件中的所有内容都已打开。

以下是我正在使用的函数文件:

对于 config.php 文件:

 <?php 
    define("DB_HOST", "MyLocalHost");
    define("DB_USER", "user");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "db");
    ?>

对于DB_Connect 文件

<?php
    class DB_Connect {

        // constructor
        function __construct() {

        }

        // destructor
        function __destruct() {
            // $this->close();
        }

        // Connecting to database
        public function connect() {
            require_once 'include/Config.php';
            // connecting to mysql
            $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysqli_error());
            // selecting database
            mysqli_select_db($con,DB_DATABASE) or die(mysqli_error());

            // return database handler
            return $con;
        }

        // Closing database connection
        public function close() {
            mysql_close();
        }

    }

    ?>

对于 DB_Functions 文件:

<?php

    class DB_Functions {

        private $db;

        //put your code here
        // constructor
        function __construct() {
            require_once 'DB_Connect.php';
            // connecting to database
            $this->db = new DB_Connect();
            $this->db->connect();
        }

        // destructor
        function __destruct() {

        }



        public function getAppointments($did) {
            $result = mysqli_query($this->db->connect(),"SELECT * FROM appointment WHERE did='$did'");
            $appointments=array();
           if($result){
                while($row = mysqli_fetch_assoc($result)) {



                $appointments[]=$row;


            }
            return $appointments; 
         }
         else{
            return false;
        }

       }

       public function getAppointmentsByJSON($did){
            echo json_encode($this->getAppointments($did));
       }


    }

    ?>

对于我的 getAppointmentsJson 文件:

<?php
//include "include/DB_Connect.php";
include "include/DB_Functions.php";

if(isset($_GET["did"])){
    if(is_numeric($_GET["did"])){
        $testObject = new DB_Functions();
        $testObject->getAppointmentsByJSON($_GET["did"]);

        echo json_encode($testObject);
    }
}

?>

我们将不胜感激所提供的帮助建议或提示。 谢谢

很抱歉通知您,空白 PHP 页面在 99% 的情况下都是 php 错误,通常是语法错误。虽然您可以在 ini 文件中启用错误报告,但我建议您在常量定义之上这样做。

error_reporting(-1); // enable error reporting on known and future defined errors
ini_set('display_errors', 'On');

在你的getAppointmentsJson中是一个错误,特别是echo json_encode($testObject,);

PHP 需要一个额外的参数给出 ,.

复制代码和复制数据库之后。

我能得出的唯一结论是,您的 getAppointmentsJson.php 脚本需要一个 get 参数。

如果您浏览到 getAppointmentsJson.php?did=1 你会看到一个输出。

另外我在你的 DB_Connect class 中编辑了两行 mysqli_error();需要 1 个参数,而你没有传递给它。

            $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysqli_error($con));
        // selecting database
        mysqli_select_db($con,DB_DATABASE) or die(mysqli_error($con));