diff --git a/api_db.php b/api_db.php index 515554fb..24624a1e 100644 --- a/api_db.php +++ b/api_db.php @@ -17,24 +17,33 @@ $data = array(); // Needs package php5-sqlite, e.g. // sudo apt-get install php5-sqlite +class FTLDB extends SQLite3 +{ + function __construct() + { + $this->open("/etc/pihole/pihole-FTL.db", SQLITE3_OPEN_READONLY); + } +} + function SQLite3_connect($trytoreconnect) { try { // connect to database - $db = new SQLite3('/etc/pihole/pihole-FTL.db'); + return new FTLDB; } catch (Exception $exception) { // sqlite3 throws an exception when it is unable to connect, try to reconnect after 3 seconds if($trytoreconnect) { sleep(3); - SQLite3_connect(false); + $db = SQLite3_connect(false); } } } -SQLite3_connect(true); -if(empty($db)) +$db = SQLite3_connect(true); + +if(!$db) { die("Error connecting to database"); }