1

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

9
  • Obviously your errors are not on as at least you have one syntax error. Commented Jan 9, 2016 at 17:24
  • What is it that you hope to get done? Convert the fetched data into a JSON array? Commented Jan 9, 2016 at 17:24
  • @HaiderAli Yes and print it out Commented Jan 9, 2016 at 17:26
  • @9it3e1 Have you tried the code after removing out the comma from json_encode(); Is it still the same? Commented Jan 9, 2016 at 17:40
  • @HaiderAli the common was a typo error while writing it here. There is still no changes Commented Jan 9, 2016 at 17:45

2 Answers 2

3

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Works Now Thank you.
1

I'm sorry to imform you but a blank PHP page is 99% of the time an error in php and usually the syntax. While you can enable error reporting in your ini file I would suggest doing that above the constant definitions in your case.

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

In your getAppointmentsJson is an error, specifically echo json_encode($testObject,);

PHP expects an additional argument giving the ,.

1 Comment

The comma was a typo error. It does show the error for the comma however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.