π§© AppML β Using AppML with SQL Databases: Connect and Query Like a Pro
π§² Introduction β Why Integrate AppML with SQL?
AppML (Application Modeling Language) thrives in scenarios where structured data is essential. While JSON and XML are great for static content, SQL databases like MySQL, PostgreSQL, or SQL Server bring dynamic power to your AppML apps.
With just a few lines of HTML and a backend script (PHP/ASP), you can load, filter, and render data from SQL tables into your AppML-powered interfaceβwithout writing JavaScript.
π― In this guide, youβll learn:
- How to set up AppML with SQL databases
- Create backend scripts to query SQL tables
- Use
sqlfilter, pagination, and sorting features - Best practices for secure and optimized SQL integration
ποΈ How AppML Communicates with SQL Databases
AppML uses a controller or appml-data reference to connect to a PHP/ASP script, which then runs an SQL query and returns a JSON response.
π Basic Flow:
[AppML HTML View]
β
[appml-data=”get-data.php”]
β [PHP/ASP Script] β [SQL Database β Returns JSON β Back to AppML]
β The database logic stays server-side while the view is managed by AppML.
π§ͺ Step-by-Step Example β Display Products from MySQL
1οΈβ£ Create MySQL Table
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
price DECIMAL(10, 2),
category VARCHAR(50)
);
2οΈβ£ Add Sample Data
INSERT INTO products (name, price, category) VALUES
('Laptop', 999.99, 'Electronics'),
('Chair', 49.99, 'Furniture');
3οΈβ£ PHP Script (get-products.php)
<?php
$conn = new mysqli("localhost", "root", "", "store");
$filter = isset($_GET["filter"]) ? "WHERE " . $_GET["filter"] : "";
$sql = "SELECT name, price, category FROM products $filter";
$result = $conn->query($sql);
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
?>
4οΈβ£ AppML View (HTML)
<div appml-data="get-products.php">
<h3>{{name}}</h3>
<p>Price: ${{price}}</p>
<small>Category: {{category}}</small>
</div>
π AppML will dynamically display all products retrieved from the SQL database.
ποΈ Adding SQL Filtering with Controller
Controller File (product-controller.js)
myAppML.onshow = function() {
myAppML.sqlfilter = "category='Electronics'";
};
Updated HTML
<div appml-data="get-products.php" appml-controller="product-controller.js">
<h3>{{name}}</h3>
<p>${{price}}</p>
</div>
β This filters data server-side and displays only βElectronicsβ category.
π Pagination and Sorting in SQL Queries
Enhance your backend with pagination support:
$start = $_GET["start"] ?? 0;
$limit = $_GET["limit"] ?? 10;
$sql = "SELECT * FROM products $filter LIMIT $start, $limit";
β οΈ You must implement the
startandlimitlogic manually, as AppML does not auto-handle pagination without backend support.
π§° Supported SQL Databases with AppML
| π’οΈ Database | β Supported via |
|---|---|
| MySQL | β PHP/MySQLi |
| PostgreSQL | β PHP pg_connect |
| SQL Server | β ASP/ADO |
| SQLite | β PHP PDO |
AppML supports any SQL database as long as your server-side script returns JSON.
π Security Tips for SQL Integration
- β
Always sanitize input using prepared statements or
mysqli_real_escape_string. - β Never expose raw SQL errors to the browser.
- β Serve data over HTTPS and protect your backend files with proper permissions.
- β Avoid sending full records if only a few fields are needed.
π§ Real-World Use Case: Admin Dashboard
<div appml-data="get-orders.php">
<h4>Order #{{order_id}}</h4>
<p>Total: ${{total}}</p>
<p>Status: {{status}}</p>
</div>
This can power your live dashboards, inventory systems, or analytics reports using SQL as a central data source.
π Summary β Recap & Key Takeaways
AppML makes it incredibly simple to connect HTML interfaces with backend SQL databases. With minimal HTML and backend scripting, you can build powerful, data-rich apps.
π Key Takeaways:
- Use
appml-datato connect AppML to a PHP/ASP script - The backend queries the SQL database and returns JSON
- Add filtering, pagination, and sorting using URL params or controllers
- Secure your SQL integration with sanitized inputs and restricted queries
βοΈ AppML + SQL is a low-code way to connect real-time databases to modern frontends.
β FAQs β Using SQL with AppML
β Does AppML connect directly to a MySQL database?
β No. You need a server-side script (PHP/ASP) to connect and return JSON.
β What is the required format for SQL data in AppML?
β
The backend should return an array of JSON objects (e.g., [{"name": "Laptop", "price": 999}]).
β Can I perform filtering using SQL queries?
β
Yes, by setting myAppML.sqlfilter in the controller and applying it in the backend script.
β Is pagination supported in AppML for SQL data?
β οΈ Not automatically. You must handle pagination logic manually in your server-side script.
β Can I update or delete SQL records via AppML?
β
Yes, using HTML forms + backend PHP/ASP scripts that handle SQL UPDATE and DELETE.
Share Now :
