From a2b1864e8d7ae7c89e9f302a33d307cbc410e869 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 20 Jul 2017 20:13:05 +0200 Subject: [PATCH] Try to reconnect to database if first attempt failed Signed-off-by: DL6ER --- api_db.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/api_db.php b/api_db.php index a4de0002..515554fb 100644 --- a/api_db.php +++ b/api_db.php @@ -17,19 +17,27 @@ $data = array(); // Needs package php5-sqlite, e.g. // sudo apt-get install php5-sqlite -$db = new SQLite3('/etc/pihole/pihole-FTL.db'); -if(!$db) - die("Cannot access database"); +function SQLite3_connect($trytoreconnect) +{ + try { + // connect to database + $db = new SQLite3('/etc/pihole/pihole-FTL.db'); + } + 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); + } + } +} +SQLite3_connect(true); -// Long-Term API functions -// if (isset($_GET['test'])) -// { -// // $results = $db->query('SELECT * FROM QUERIES order by TIMESTAMP ASC LIMIT 2'); -// $results = $db->query('SELECT TIMESTAMP,TYPE,DOMAIN,CLIENT,STATUS FROM QUERIES order by TIMESTAMP ASC'); -// echo "N=".$result->numColumns; -// while ($row = $results->fetchArray()) -// var_dump($row); -// } +if(empty($db)) +{ + die("Error connecting to database"); +} if (isset($_GET['getAllQueries']) && $auth) {