Skip to content

Suggestion to make portable, faster and streamline for your read.php example. #2

Description

@jhersey29

Thank your for your work and simple API starter. I wanted to recommend a few small changes that I ran into when getting it going. I know you designed this for just MySql. I ported it over to Sql Server and the rowCount in PDO does not work with Sql Server. This code will work in both platforms and removes some extraneous unnecessary code and improves the performance.

`<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once '../config/database.php';
include_once '../class/employees.php';
    
$database = new Database();
$db = $database->getConnection();

$items = new Employee($db);
$stmt = $items->getEmployees();
    
$employeeArr = array();
$employeeArr["body"] = array();    

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){             
    $employeeArr["body"][] = $row;        
}     
$employeeArr["itemCount"] = count($employeeArr["body"]);

if($employeeArr["itemCount"] > 0 ){
    echo json_encode($employeeArr);
} else {
    http_response_code(404);
    echo json_encode(
        array("message" => "No record found.")
    );
}`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions