-1

EDIT

For heaven's sake, this is not a duplicate. Do not close it as such. I'm not inquiring about what the error means. I'm more interested in knowing why this is giving me an error in this case while it works fine when used at other places.

For a query such as:

$this->db->select('user_id');
$this->db->from("members");
$this->db->where(array("username"=>$post->un, "password"=>sha1($post->pw)));
$query = $this->db->get();  
if($query->num_rows() == 1)
{
   // some logic goes here
}

I'm getting the following error.

Call to a member function num_rows() on boolean

If I use $this->db->last_query(), I'm getting the following result:

SELECT `user_id`
FROM `members`
WHERE `username` = 'adg'
AND `password` = '3a1c21a559ed42d6ce17c0b8205b6bda2465c2a8'

The query is 100% correct and when run in MySql console, returns an empty set (0 rows) which is fine. But why then is it returning boolean when used in the code.

By the way, I'm using num_rows() on the $query object in various other places and that seem to be working fine.

CI version is 3.1.3

Update

  • var_dump($query) is printing out bool(false)
Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119
  • 1
    try to use var_dump($query) for see a object This function prints the variable's content on the page – Gianfrancesco Aurecchia Jun 05 '17 at 09:42
  • 1
    Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – CBroe Jun 05 '17 at 09:47
  • Use like `if($query && $query->num_rows() == 1)` to handle this situation – Parag Soni Jun 05 '17 at 09:47
  • @CBroe I beg to differ. It's not about "what does it mean". It's about "why it works in one place but doesn't at the other" – asprin Jun 05 '17 at 09:48
  • @ParagSoni Yes, that will solve the issue but my question still remains. Why is this particular instance giving a problem but working fine with other cases? – asprin Jun 05 '17 at 09:48
  • num_rows() first perfrom the query then check how many rows – Moyed Ansari Jun 05 '17 at 09:49
  • 2
    It _is_ about what does it mean - because it means that your query failed. If you pretend that you already knew that, then I would expect that you had been reading up on how to debug _why_ your query failed at this point as well. – CBroe Jun 05 '17 at 09:50
  • @CBroe Read the entire OP. The sql query generated is fine. It runs fine in MySql console too. There is no syntax error. This is sort of a bug. – asprin Jun 05 '17 at 09:52
  • And `_error_message` and `_error_number` have what to say ...? (Or `error`, depending on CI version.) – CBroe Jun 05 '17 at 09:55
  • 1
    You have to get the underlying sql error - till then we can only guess. Btw, your question **is** a duplicate of what @CBroe has linked in, since it contains no MySQL error message and the debugging steps are described in the linked topic. – Shadow Jun 05 '17 at 09:56
  • @asprin Because data which match to your query is does not exist, so instead of an object return value is boolean `false` and here you are calling a method of an object as`$query->num_rows()` but without an object it throws an error. – Parag Soni Jun 05 '17 at 09:56
  • @asprin Just tip would not use sha1 for password use something like http://php.net/manual/en/function.password-hash.php and to verify it callback http://php.net/manual/en/function.password-verify.php –  Jun 05 '17 at 09:57
  • 1
    @ParagSoni that's not how it works, an _empty_ query result is not an _error_. – CBroe Jun 05 '17 at 09:57
  • @ParagSoni nope, that's not correct. – Shadow Jun 05 '17 at 09:58
  • please check your autoload.php file. $autoload['libraries'] = array('database'); – Bhavin Thummar Jun 05 '17 at 10:50

1 Answers1

0

It's not well documented but when a query fails a boolean might be returned instead of a CI_DB_result object. That is what is happening to you. My first guess as to why it happens in your example is that some piece of data is being improperly escaped - most likely in the where statement. Try this.

$this->db->where(array("username"=>$post->un, "password"=>sha1($post->pw)), NULL, FALSE);

And see if that helps.

I have, on occasion, when queries are complicated, had to resort to the following check on my results like this.

if($query instanceof CI_DB_result && $query->num_rows() == 1) {...

just to make sure I'm not trying to "Call to a member function num_rows() on boolean"

DFriend
  • 8,869
  • 1
  • 13
  • 26