From: Mischa POSLAWSKY Date: Thu, 17 Oct 2019 03:52:48 +0000 (+0200) Subject: nieuws/replies: database class with query method X-Git-Tag: v4.1~12 X-Git-Url: http://git.shiar.nl/minimedit.git/commitdiff_plain/75cbd3cb3b7e08963686ca81976c086fd531a8b8 nieuws/replies: database class with query method Code cleanup preparing for more extensive database support (where default php is somewhat lacking). --- diff --git a/database.inc.php b/database.inc.php index 6e040d4..aa864d0 100644 --- a/database.inc.php +++ b/database.inc.php @@ -1,4 +1,21 @@ PDO::ERRMODE_EXCEPTION ]; -$Db = new PDO($dsn, NULL, NULL, $options); +$Db = new DB($dsn); + +class DB +{ + public $dbh; + + function __construct($config, $options = []) + { + $options += [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]; + $this->dbh = new PDO($config, NULL, NULL, $options); + } + + function query($sql, $params = []) + { + $stmt = $this->dbh->prepare($sql); + $stmt->execute($params); + return $stmt; + } +} diff --git a/widget/nieuws/replies.php b/widget/nieuws/replies.php index 54e552a..13533c9 100644 --- a/widget/nieuws/replies.php +++ b/widget/nieuws/replies.php @@ -8,8 +8,9 @@ if ($_POST) { try { $html = nl2br(htmlspecialchars($_POST['reply'])); $html = "

$html

"; - $query = $Db->prepare('INSERT INTO comments (page, message, author) VALUES (?, ?, ?)'); - $query->execute([ $Page, $html, $User->login ]); + $query = $Db->query('INSERT INTO comments (page, message, author) VALUES (?, ?, ?)', [ + $Page, $html, $User->login + ]); if (!$query->rowCount()) { throw new Exception('Fout bij opslaan'); } @@ -20,8 +21,7 @@ if ($_POST) { } } -$query = $Db->prepare('SELECT * FROM comments WHERE page = ? ORDER BY created'); -$query->execute([$Page]); +$query = $Db->query('SELECT * FROM comments WHERE page = ? ORDER BY created', [$Page]); print '