This example sets up the grid to use the serverSide model and sends parameters (startRow, endRow, sortModel, filterModel) to PHP. javascript
This comprehensive guide demonstrates how to build an updated, secure, and modern PHP backend that integrates seamlessly with AG Grid’s client-side interface. Architecture Overview
: A modern HTML5/JavaScript interface that initializes AG Grid, defines columns, and requests data.
This HTML file includes the AG Grid library and creates a simple grid with three columns. It then fetches data from the "grid.php" script and passes it to the AG Grid.
// Close the database connection $conn->close(); aggrid php example updated
<?php // api/get-rows.php header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, OPTIONS'); if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit(0);
// Add server-side filtering and sorting if (isset($_GET['filter'])) $filter = $_GET['filter']; $data = []; // Apply the filter foreach ($data as $row) if (strpos($row['name'], $filter) !== false
Need help? Copy the code above, adjust your database credentials, and you’ll have an enterprise-grade datagrid running in under 30 minutes. This is the most current and detailed AG Grid PHP example available online as of 2025.
If the PHP example constructs database queries based on AG Grid's request parameters (like filtering or sorting), there is a high risk of SQL Injection if not handled correctly. This example sets up the grid to use
const columnDefs = [ field: "id", sortable: true, filter: true , field: "name", editable: true , field: "category", editable: true , field: "price", editable: true ]; const gridOptions = columnDefs: columnDefs, // Capture edits to update the database onCellValueChanged: (params) => fetch('update.php', method: 'POST', body: JSON.stringify(params.data) ); ; const gridDiv = document.querySelector('#myGrid'); const api = agGrid.createGrid(gridDiv, gridOptions); // Fetch initial data from PHP fetch('fetch.php') .then(response => response.json()) .then(data => api.setGridOption('rowData', data)); Use code with caution. 3. The Backend: PHP & MySQL API
Implementing with a PHP backend is a common requirement for enterprise applications handling large datasets. This guide provides a fully updated, 2026-compliant approach for connecting AG-Grid to a MySQL database using PHP, focusing on Server-Side Row Model (SSRM) for pagination , sorting , and filtering . Why Use Server-Side Data with PHP?
// Bind filter params foreach ($params as $key => $val) $stmt->bindValue($key, $val);
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>AG Grid PHP Example</title> <script src="https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js"></script> <link rel="stylesheet" href="https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css" /> <link rel="stylesheet" href="https://unpkg.com/ag-grid-community/dist/styles/ag-theme-alpine.css" /> <style> html, body, #myGrid height: 100%; margin: 0; width: 100%; </style> </head> <body> <div id="myGrid" class="ag-theme-alpine"></div> This HTML file includes the AG Grid library
: Setting row data after initial load is safely achieved using the updated setGridOption('rowData', data) setter pattern.
When a cell is edited in the grid, this script receives the updated row data.
<?php // Configuration $dbHost = 'localhost'; $dbUsername = 'your_username'; $dbPassword = 'your_password'; $dbName = 'your_database';