-1

So, I have installed codeigniter in my VPS Ubuntu 16. When I want to login to mydomain/admin, I enter my details. If I press login, I get the error below plus other members can't create accounts.

An uncaught Exception was encountered
Type: Error
Message: Call to a member function result() on boolean
Filename: /var/www/html/application/models/backend/dashboard/Dashboard_model.php
Line Number: 128

Backtrace:

File: /var/www/html/application/controllers/backend/dashboard/Home.php
Line: 27
Function: monthlyInvestment

File: /var/www/html/index.php
Line: 315
Function: require_once

My code at line 128:

return $query = $this->db->query("SELECT MONTHNAME(\`invest_date\`) as month, SUM(\`amount\`) as invest FROM \`investment\` GROUP BY YEAR(\`invest_date\`), MONTH(\`invest_date\`)")->result();

At line 27:

$data['monthlyInvestment'] = $this->dashboard_model->monthlyInvestment();

At line 315:

require_once BASEPATH.'core/CodeIgniter.php';
acarlstein
  • 1,799
  • 2
  • 13
  • 21
Cobrazor
  • 1
  • 1
  • When you don't know how to grab the actual query error, copy-paste your query into phpmyadmin and execute it -- it will show you the error plainly. – mickmackusa May 06 '19 at 04:36

1 Answers1

0

This happens because your query returns a boolean. In this case FALSE because of some database error. My best guess is the error is because you tried to escape the backticks in your query string. Change it as follows, but don't return it immediately.

$query = $this->db->query("SELECT MONTHNAME(`invest_date`) as month, SUM(`amount`) as invest FROM `investment` GROUP BY YEAR(`invest_date`), MONTH(`invest_date`)");

// check that $query isn't FALSE, if not return result() else return empty array.
return $query !== FALSE ? $query->result() ? array();

Why return an empty array? That is the same thing that result() would return if the query had not failed but there was no data to return.

DFriend
  • 8,869
  • 1
  • 13
  • 26