Single method for different parameterized mysql command I currently have these two methods. One method returns all records, while the other one returns only a specific record.

function GetLeaveRequestDetailsAll($leaveCurrentStatus){

    global $connPDO;

    $sql = "select *, emp.Employee_Name, emp.Designation, emp.Emp_Dept_Category, lt.Leave_Name
            from tbl_emp_leave_details eld
            Inner join tbl_leave_types lt
            ON lt.leave_id = eld.leave_type
            Inner Join employee emp
            on emp.PK = eld.emp_id
            Inner JOIN dept_category dc
            ON emp.Emp_Dept_Category = dc.PK
            where eld.leave_current_status_id = :leave_current_status_id";

    $binds = array(
        ":leave_current_status_id"   =&gt;  $leaveCurrentStatus
    );

    $statementEntryCount = $connPDO-&gt;prepare($sql);

    $statementEntryCount-&gt;execute($binds);
    $queryData = $statementEntryCount-&gt;fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($queryData);
}


function GetLeaveRequestDetailsById($leaveCurrentStatus, $leaveDetailId){

    global $connPDO;

    $sql = "select *, emp.Employee_Name, emp.Designation, emp.Emp_Dept_Category, lt.Leave_Name
            from tbl_emp_leave_details eld
            Inner join tbl_leave_types lt
            ON lt.leave_id = eld.leave_type
            Inner Join employee emp
            on emp.PK = eld.emp_id
            Inner JOIN dept_category dc
            ON emp.Emp_Dept_Category = dc.PK
            where eld.leave_current_status_id = :leave_current_status_id
            And eld.leave_detail_id = :leave_detail_id";

    $binds = array(
        ":leave_current_status_id"   =&gt;  $leaveCurrentStatus,
        ":leave_detail_id"   =&gt;  $leaveDetailId
    );

    $statementEntryCount = $connPDO-&gt;prepare($sql);

    $statementEntryCount-&gt;execute($binds);
    $queryData = $statementEntryCount-&gt;fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($queryData);
}


As you can see, both are doing the same work with a small difference, which is leave_detail_id. How can I convert these two functions into a single parameterized method to handle both queries?
If I assume that leave_detail_id contains an auto-increment value, starting at 1, you can do:

function queryDatabase($query, $bindings = [])
{
    global $connPDO;
    // prepare statement 
    $statement = $connPDO-&gt;prepare($query);
    // execute statement    
    $statement-&gt;execute($bindings);
    // return result in associative array
    return $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
}

function getLeaveRequestDetails($leaveCurrentStatus, $leaveDetailId = 0)
{
    // build query
    $query = "SELECT *, 
                     emp.Employee_Name, 
                     emp.Designation, 
                     emp.Emp_Dept_Category, 
                     lt.Leave_Name
              FROM tbl_emp_leave_details eld
              INNER JOIN tbl_leave_types lt ON lt.leave_id = eld.leave_type
              INNER JOIN employee emp ON emp.PK = eld.emp_id
              INNER JOIN dept_category dc ON emp.Emp_Dept_Category = dc.PK
              WHERE eld.leave_current_status_id = :leave_current_status_id";
    // define bindings
    $bindings = [":leave_current_status_id" =&gt; $leaveCurrentStatus];
    // should details id be use? 
    if ($leaveDetailId &gt; 0) {
          $query .= " AND eld.leave_detail_id = :leave_detail_id";
          $bindings[":leave_detail_id"] = $leaveDetailId;
    }
    // query database and return result
    return queryDatabase($query, $bindings);
}


I used a default function argument value to set $leaveDetailId to zero, if it is absent, when the function is called. If it does get a value bigger than zero the condition is added to the query and the value is bound.

I also removed the actual database querying from this function and put it in a separate function. The reason is that this code can probably be used more often in your scripts. Instead of repeating it every time you can now call queryDatabase().

Finally I removed the echo json_encode(); from the function. The function name starts with "get" and that almost always implies something will be returned. Also, echoing JSON would restrict this function to that task alone. Now you can still use it to echo JSON, but you can also use it to do something else with the result. A call, returning JSON, would look something like this:

echo json_encode(getLeaveRequestDetails("fallen",15));


As you can see I've now split your code into more parts than you had before. That's not a bad thing. Now each part performs a single function: Querying the database, getting details of a leave, and echoing JSON. This makes your code easier to read.

In summary:


Do not repeat yourself (DRY). This was your reason for asking this question.
functions should be created with reusability in mind, even the specialized ones. That's the whole reason for their existance. 
functions should only perform one task (Single responsibility principle).