doujing2017 2016-01-09 17: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 18: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条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败