CodeIgniter Forums
DB values returning as wrong types - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: DB values returning as wrong types (/showthread.php?tid=14013)



DB values returning as wrong types - El Forum - 12-15-2008

[eluser]Phil Sturgeon[/eluser]
Values stored in the database as integers or floats are being returned as strings. I can understand why, but it shouldn't happen. It really messes with my function overloading Confusedhut:


DB values returning as wrong types - El Forum - 12-16-2008

[eluser]Derek Allard[/eluser]
What's the bug report? How are you inserting and then removing?


DB values returning as wrong types - El Forum - 12-17-2008

[eluser]Phil Sturgeon[/eluser]
Just any normal Active Record return using get(), do a var_dump on the results and you will see any number from the database returns as a string. I think the database library just assumes everything is a string, and doesn't treat integers, floats, decimals, etc any differently.

I know PHP is not a type-based language, and for most actions this doesn't make any different. But if you are making models that do get_type on the params, and try to act differently according to the type, you will get bumped as any number passed as an argument from the database will show as a string.


DB values returning as wrong types - El Forum - 12-17-2008

[eluser]m4rw3r[/eluser]
I think that the PHP extensions that handle the database connections always returns strings.
But I'm not sure.


DB values returning as wrong types - El Forum - 12-18-2008

[eluser]Derek Allard[/eluser]
Hm, can you typecast?

Code:
$mynumber = (int)$this->db->get('table')->row()->number_field;



DB values returning as wrong types - El Forum - 12-18-2008

[eluser]Phil Sturgeon[/eluser]
Yea that's what I'm doing at the moment, but it's not too sexy in my controllers. The reason I was trying type based function parameters was to keep controller code down to a minimum.

It's not a major issue, just wondering if it can be easily addressed.


DB values returning as wrong types - El Forum - 12-18-2008

[eluser]Phil Sturgeon[/eluser]
And yea after looking into it is a "bug" in mysql_fetch_object(). Looks like the only way to do it would be to look through every field, check the type then overwrite it after a typecast... not fun on the CPU, sod that.