Aggrid Php Example Updated Updated -

: Never concatenate raw strings inside your PHP SQL logic. Utilize standard PDO parameter binding ( $stmt->execute([':id' => $id]) ) to mitigate SQL injection vulnerabilities.

Create a PHP backend that will interact with the AG Grid application. Our backend will consist of two files: grid.php and data.php .

<div id="myGrid" class="ag-theme-alpine"></div>

This frontend code configures AG Grid to use the serverSide row model, linking it to the PHP endpoint created above. javascript

For very high-traffic applications, consider caching the result of the totalRows query for a few seconds. aggrid php example updated

// Close the database connection $conn->close();

Offers built-in filtering, sorting, column resizing, pinning, grouping, and row pivoting.

: A getRows function in JS that POST s the grid's request object to your PHP endpoint.

While the example above uses simple string interpolation for brevity, always use prepared statements ( $pdo->prepare ) for filtering values to prevent SQL injection in production. : Never concatenate raw strings inside your PHP SQL logic

Updated AG Grid PHP Example: High-Performance Data Handling in 2026

$value) if ($value['filterType'] == 'text') $whereParts[] = "$key LIKE '%" . $conn->real_escape_string($value['filter']) . "%'"; // Additional filter types (set, number, date) would go here $whereSql = implode(" AND ", $whereParts); // 5. Get Total Count (for Server-Side Model to know the total rows) $countSql = "SELECT COUNT(*) as total FROM employees WHERE $whereSql"; $countResult = $conn->query($countSql); $totalRows = $countResult->fetch_assoc()['total']; // 6. Fetch Data $sql = "SELECT * FROM employees WHERE $whereSql $orderBy LIMIT $startRow, $limit"; $result = $conn->query($sql); $rows = []; while ($row = $result->fetch_assoc()) $rows[] = $row; // 7. Return Result to AG Grid echo json_encode([ 'rows' => $rows, 'lastRow' => $totalRows ]); $conn->close(); ?> Use code with caution. 3. The Updated Frontend Example (JavaScript)

if (!empty($conditions)) $where = " WHERE " . implode(' AND ', $conditions);

<?php header('Content-Type: application/json'); Our backend will consist of two files: grid

$filter) if ($filter['filterType'] === 'text') $sql .= " AND $col LIKE :$col"; $params[$col] = '%' . $filter['filter'] . '%'; // Apply Sorting if (!empty($request['sortModel'])) $sortCol = $request['sortModel'][0]['colId']; $sortDir = $request['sortModel'][0]['sort']; $sql .= " ORDER BY $sortCol " . ($sortDir === 'desc' ? 'DESC' : 'ASC'); // 4. Get Total Count (For Pagination) $countSql = str_replace("SELECT *", "SELECT COUNT(*) as total", $sql); $stmtCount = $pdo->prepare($countSql); $stmtCount->execute($params); $totalRows = $stmtCount->fetch(PDO::FETCH_ASSOC)['total']; // 5. Apply Pagination (LIMIT/OFFSET) $sql .= " LIMIT :limit OFFSET :offset"; $stmt = $pdo->prepare($sql); foreach ($params as $key => $val) $stmt->bindValue($key, $val); $stmt->bindValue('limit', (int)$pageSize, PDO::PARAM_INT); $stmt->bindValue('offset', (int)$startRow, PDO::PARAM_INT); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // 6. Return Data echo json_encode([ 'rows' => $rows, 'totalRows' => (int)$totalRows ]); Use code with caution. 3. Key Updates in 2026 Implementation

// Define the grid columns $columns = [ ['headerName' => 'Name', 'field' => 'name'], ['headerName' => 'Email', 'field' => 'email'], ['headerName' => 'Department', 'field' => 'department'] ];

This guide bridges the gap between the frontend JavaScript grid and the backend PHP server, covering the two most common scenarios: and Inline Editing .

: Text fields deploy the LIKE command safely bundled inside parameters while number values map directly onto mathematical comparisons, adapting safely to varied input schemas. If you need help expanding this setup, tell me: