How can SQL queries be integrated into a TPL system for a CMS?

To integrate SQL queries into a TPL system for a CMS, you can create a separate PHP file that handles all database interactions and query executions. This file can be included in your TPL templates where needed to fetch data from the database. By separating the database logic from the presentation layer, you can maintain a clean and organized codebase.

// db.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

function runQuery($sql) {
    global $conn;
    $result = $conn->query($sql);
    return $result->fetch_assoc();
}
?>

// tpl_template.php
<?php
include 'db.php';

$sql = "SELECT * FROM table_name WHERE condition";
$data = runQuery($sql);

// Display data in the template
echo $data['column_name'];
?>