nieuws/replies: insert method to add database rows
[minimedit.git] / database.inc.php
1 <?php
2 $dsn = require '.dbconfig.inc.php';
3 $Db = new DB($dsn);
4
5 class DB
6 {
7         public $dbh;
8
9         function __construct($config, $options = [])
10         {
11                 $options += [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ];
12                 $this->dbh = new PDO($config, NULL, NULL, $options);
13         }
14
15         function query($sql, $params = [])
16         {
17                 $stmt = $this->dbh->prepare($sql);
18                 $stmt->execute($params);
19                 return $stmt;
20         }
21
22         function insert($table, $row)
23         {
24                 $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
25                         '"'.$table.'"',
26                         implode(', ', array_keys($row)),
27                         implode(', ', array_fill(0, count($row), '?'))
28                 );
29                 return $this->query($sql, array_values($row));
30         }
31 }