Jump to content

Redirect to installer if database is empty - Codeigniter 3


Recommended Posts

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 by Moorcam
  • Moorcam changed the title to Redirect to installer if database is empty - Codeigniter 3

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.

  • Great Answer 1

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... 

 

  • 2 weeks later...
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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.