doujing2017 2016-01-09 09:18
浏览 137
已采纳

使用JSON解析MySQL数据

Am using MAMP local host server and am trying to get the data from the table below

enter image description here

I don't get any syntax errors or stack errors but when a blank browser page and yes the display_errors and all are all turned On in the php.in file.

Below are the function files am using:

For the config.php file :

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

For DB_Connect File

<?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();
        }

    }

    ?>

For DB_Functions File:

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


    }

    ?>

And for my getAppointmentsJson file :

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

?>

And help advices or tips provided will be greatly appreciated. Thank you

展开全部

  • 写回答

2条回答 默认 最新

  • doukan5332 2016-01-09 10:08
    关注

    After replicating your code and replicating the database.

    The only conclusion i can come to is that, your getAppointmentsJson.php script expects a get parameter.

    If you browsed to getAppointmentsJson.php?did=1 you would see an output.

    Also i edited two lines in your DB_Connect class as mysqli_error(); expects exactly 1 parameter and you didn't pass it one.

                $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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部