Welcome Guest, Not a member yet? Register   Sign In
Value of an integer field returned as string
#1

[eluser]fnick[/eluser]
Following code retrieves value of a TINYINT-type field as string:
Code:
$row = $result->row_array();
$val = $row['colname'];

So I have to cast it to integer before the value can be safely compared to 1 with "===" operator.
Code:
if ((int)$val === 1)
{
    // code
}

Is it wrong to expect an integer at this point?
#2

[eluser]Massaki[/eluser]
When you use result_array() or row_array(), every field is treated as string
#3

[eluser]fnick[/eluser]
Aha! So if I use this method

Code:
$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}

I get, as written, objects -> INT's STRING's BOOLEAN's etc.?
#4

[eluser]Aken[/eluser]
No. All data returned from MySQL drivers in CodeIgniter (that includes mysql, mysqi, and pdo) all return strings, regardless of the column type in the DB or the method you use to fetch the results. You'll need to continue typecasting your code as you exampled, or write some kind of driver or wrapper of your own that does the typecasting.
#5

[eluser]fnick[/eluser]
Well, thank you for your answer. That would be my next (and firstBig Grin) request of improvement: to make Database Library return data in the real format. Well, I would not speak for all developers, but this feature could prevent a lot of confusion.




Theme © iAndrew 2016 - Forum software by © MyBB