Moorcam Posted November 23, 2023 Share Posted November 23, 2023 (edited) Hi guys, If my application is not installed, and you go to the main website, it throws the following error: Message: Call to a member function first_row() on bool Now, this is obviously because the database is empty. What I would like to do, instead of showing this error, I would like to automatically redirect to the install directory (if it exists). In Model_common, the following function is what is throwing the error: public function all_setting() { $query = $this->db->query("SELECT * from tbl_settings WHERE id=1 ORDER BY id"); return $query->first_row('array'); } I have tried this but to no avail in Home.php controller under function index if (empty($data['setting'])) { redirect('../install/'); } If anyone could help with this I would appreciate it. Cheers Dan Edited November 23, 2023 by Moorcam Quote Link to comment https://forums.phpfreaks.com/topic/317471-redirect-to-installer-if-database-is-empty-codeigniter-3/ Share on other sites More sharing options...
kicken Posted November 23, 2023 Share Posted November 23, 2023 I'm not familiar with CI3, but assuming it has a single entry point file, it'd probably be best to add an explicit check there rather than in places your DB is used. Quote Link to comment https://forums.phpfreaks.com/topic/317471-redirect-to-installer-if-database-is-empty-codeigniter-3/#findComment-1613068 Share on other sites More sharing options...
gizmola Posted November 24, 2023 Share Posted November 24, 2023 This is not a panacea, but you could change the all_setting function to this: public function all_setting() { $query = $this->db->query("SELECT * from tbl_settings WHERE id=1 ORDER BY id"); if ($query) { return $query->first_row('array'); } else { return array(); } } Then your modification would work. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317471-redirect-to-installer-if-database-is-empty-codeigniter-3/#findComment-1613073 Share on other sites More sharing options...
benanamen Posted November 24, 2023 Share Posted November 24, 2023 The ORDER BY is pointless. You are only specifying a single ID in the WHERE clause. Update the method to return false if the query fails or if there are no results. You want to fail fast with a "guard clause". In your Home method check for false and redirect to the install if the condition matches. public function all_setting() { $query = $this->db->query("SELECT * FROM tbl_settings WHERE id=1"); if ($query === false || $query->num_rows() === 0) { return false; } return $query->first_row('array'); } if ($data['setting'] === false) { redirect('../install/'); } // Other code... Quote Link to comment https://forums.phpfreaks.com/topic/317471-redirect-to-installer-if-database-is-empty-codeigniter-3/#findComment-1613074 Share on other sites More sharing options...
Moorcam Posted December 9, 2023 Author Share Posted December 9, 2023 On 11/24/2023 at 11:39 AM, gizmola said: This is not a panacea, but you could change the all_setting function to this: public function all_setting() { $query = $this->db->query("SELECT * from tbl_settings WHERE id=1 ORDER BY id"); if ($query) { return $query->first_row('array'); } else { return array(); } } Then your modification would work. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/317471-redirect-to-installer-if-database-is-empty-codeigniter-3/#findComment-1613420 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.