Welcome Guest, Not a member yet? Register   Sign In
Unable to evaluate boolean fields with result_array()?
#1

[eluser]Unknown[/eluser]
I'm experiencing a weird issue with CI where I'm using associative arrays but for some reason cannot work with boolean fields. Here is the query code:


Code:
$this->db->where('id', $id);
  if ($query = $this->db->get($this->table, 1))
  {
    if ($query->num_rows() > 0)
    {
        $result = $query->result_array();
        return $result[0];
    }  
  }


The record is returned just fine, but there are several boolean fields and when they are false (0) in the db (mysql), in the associate array they evaluate to true if you use something like:

$data = (execute above code);

$field = $data['field'];

if ($field == true) {} // this evaluates to true even though in the db it's false!

BUT they evaluate to 0 (false) if you do something like:

(int)$field

I'm using CI 2.0.3 with PHP 5.3.

Thanks!



#2

[eluser]Unknown[/eluser]
Figured it out. Was using bit in mySQL instead of actual boolean fields Smile
#3

[eluser]Kyle Johnson[/eluser]
When I use booleans in PHP (and presumably in my database) I try to make sure the datatypes match as well.

Code:
if($field === TRUE) { // 3 = signs instead of 2
  // do something
}

Because true == any non-zero number in PHP. Well, if it isn't 0, or empty, it's true.

http://php.net/manual/en/language.types.boolean.php
Code:
<?php
var_dump((bool) "");        // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)
?>
#4

[eluser]Robert Fulton[/eluser]
The record is returned just fine.

[url="http://movie.stepaheadzone.com"]mediafire movies download[/url]




Theme © iAndrew 2016 - Forum software by © MyBB