<?php
include("../incl/functions.php");
$dbloc = $_SERVER["DOCUMENT_ROOT"] . "/me/db/rasmussteen.sqlite";
$dsn = "sqlite:$dbloc";
if (check_libs()) {
    $db = connect_database($dsn);
    $sql = "SELECT * FROM comments WHERE parent = ? ORDER BY date(time) DESC LIMIT ?";
    $stmt = $db->prepare($sql);
    $params = ["kmom01", 2]; # , $maxResults
    try {
        $stmt->execute($params);
        $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo "<p>The result contains " . count($res) . " rows.</p>";
        $rows = null;
        foreach ($res as $row) {
            $rows .= "<div class=\"comment\">";
            $rows .= "<p>{$row["name"]} wrote on {$row["time"]}</p>";
            $rows .= "<p>{$row["text"]}</p>";
            $rows .= "</div>";
        }
        echo $rows;
    } catch (PDOException $e) {
        echo "<p>Failed to get comments.</p>";
        throw $e;
    }
}
