Convert regular php select to phpmaker code

I need to convert this code to phpmaker code . esp the json decode and encode part

$input = json_decode(file_get_contents('php://input'), true);
//print_r($input);
if (isset($input['category_id'])) {
    $categoryId = $input['category_id'];
    if($categoryId > 0){
        // Prepare the SQL statement
        $stmt = $pdo->prepare("SELECT * FROM pos_product WHERE category_id = :category_id ORDER BY product_name ASC");
        $stmt->bindParam(':category_id', $categoryId, PDO::PARAM_INT);
    } else {
        // Prepare the SQL statement
        $stmt = $pdo->prepare("SELECT * FROM pos_product ORDER BY product_name ASC");
    }

    // Execute the statement
    $stmt->execute();

    // Fetch all results
    $products = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Return results as JSON
    echo json_encode($products);
}

this is the function that uses ajax to send data to the code above

function load_category_product(category_id = 0)
{
    fetch('orderajax', {
        method: 'POST',
        
        body: JSON.stringify({ category_id: category_id })
    })
    .then(response => response.json())
    .then(data => {
        if (data.error) {
            console.error('Error:', data.error);
        } else {
            let html = '';
if (isset($input['category_id'])) {
    $categoryId = $input['category_id'];
   

    if($categoryId > 0){
       $products = ExecuteRows("SELECT * FROM pos_product WHERE category_id = '$categoryId' ORDER BY product_name ASC");
    } 
	
	else {
        // Prepare the SQL statement
       $products = ExecuteRows("SELECT * FROM pos_product ORDER BY product_name ASC");
    }



WriteJson($products);

works and i got json data response . But the issue is that i get bunch of html codes and white space with the response and this is crashing my code.

[{"product_id":66,"category_id":4,"product_name":"Assorted peppersoup","product_image":"null.png","product_cost":"0.00","product_price":"5000.00","product_status":"Available"},{"product_id":5,"category_id":2,"product_name":"Jollof Rice","product_image":"food-removebg-preview.png","product_cost":"0.00","product_price":"1000.00","product_status":"Available"},{"product_id":3,"category_id":1,"product_name":"Malt","product_image":"soda-removebg-preview(2).png","product_cost":"0.00","product_price":"1000.00","product_status":"Available"},{"product_id":4,"category_id":1,"product_name":"Malt2","product_image":"soda-removebg-preview.png","product_cost":"0.00","product_price":"1000.00","product_status":"Available"},{"product_id":65,"category_id":3,"product_name":"Peppered Goat meat","product_image":"null.png","product_cost":"0.00","product_price":"4000.00","product_status":"Available"},{"product_id":2,"category_id":2,"product_name":"Pizza","product_image":"food-removebg-preview(2).png","product_cost":"0.00","product_price":"345.00","product_status":"Available"},{"product_id":1,"category_id":2,"product_name":"Rice","product_image":"menu-removebg-preview.png","product_cost":"0.00","product_price":"1000.00","product_status":"Available"},{"product_id":67,"category_id":2,"product_name":"Rice2","product_image":"null.png","product_cost":"0.00","product_price":"3000.00","product_status":"Available"}]
<!DOCTYPE html>
<html data-bs-theme="light">
    <head>
        <title>POS</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="/vintopos/css/select2.min.css?v=24.16.8">
        <link rel="stylesheet" href="/vintopos/css/select2-bootstrap5.min.css?v=24.16.8">
        <link rel="stylesheet" href="/vintopos/plugins/fontawesome-free/css/all.min.css?v=24.16.8">
        <link rel="stylesheet" href="/vintopos/adminlte3/css/adminlte.min.css?v=24.16.8">
        <link rel="stylesheet" href="/vintopos/css/vintopos.min.css?v=24.16.8">
        <script data-pace-options='{"ajax":{"trackMethods":["GET","POST"],"ignoreURLs":["\/session?"]},"eventLag":false}' src="/vintopos/js/pace.js?v=24.16.8"></script>
        <!-- Single quotes for data-pace-options -->
        <script src="/vintopos/js/ewcore.min.js?v=

console error : sales:793 Fetch error: SyntaxError: Unexpected non-whitespace character after JSON at position 1405 (line 1 column 1406)

You may create your own API action, get the “category_id” from route, build the SQL, and return JSON by ExecuteJson($sql).

works well thank you