<?php
    // attempt to connect to the DB
    $dbconn = mysqli_connect("localhost", "root", "", "famouscat");
    if ($dbconn->connect_error > 0) {
        die('Could not connect: ' . $dbconn->connect_error);
        exit(); // error page
    }
 
    // check for cat data
    $sql = $dbconn->prepare("SELECT CatID as CatID,
									cats as cats
									from famouscat");	
    
    // now execute the query
    $sql->execute();
    $result = $sql->get_result();
    
    // create the array to hold the list of famous cats    
    $famouscat = array();  
    // check if we have data 
    if($sql->affected_rows == 0)
    {
        // set response code - 404 Not found
        http_response_code(404);

        // tell the user no cats found
        echo json_encode(
            array("message" => "No Cats Found.")
        );
    }
    else
    {    
        while($row = $result->fetch_assoc())
        {
            $famouscat[] = $row;
        }
        
        // set response code 
        http_response_code(200);

        // show products data in json format
        
        header('Content-Type: application/json');
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
        $result = [
            'message' => 'Success',
            'famouscat' => $famouscat,
        ];
        echo json_encode($result, JSON_PRETTY_PRINT);
    }
    
    //close connection
    mysqli_close($dbconn);
    
    
 ?>
 
 