CodeIgniter Forums
Get data in type from DB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Get data in type from DB (/showthread.php?tid=86278)



Get data in type from DB - motoroller - 01-26-2023

+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| term | varchar(255) | NO | | NULL | |
| level | int(11) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+


Result

array(4) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["term"]=>
string(11) "Mesoamerica"
["level"]=>
string(1) "1"
}
[1]=>
array(3) {
["id"]=>
string(1) "2"
["term"]=>
string(6) "Mexico"
["level"]=>
string(1) "1"
}
[2]=>
array(3) {
["id"]=>
string(1) "3"
["term"]=>
string(10) "indigenous"
["level"]=>
string(1) "2"
}
[3]=>
array(3) {
["id"]=>
string(1) "4"
["term"]=>
string(5) "ruins"
["level"]=>
string(1) "2"
}
}

but i need ID if integer instead


RE: Get data in type from DB - kilishan - 01-26-2023

MySQL will return most things as a string since each programming language has a different set of primitive types.

If you're using Entities, you can cast the column to an int as described in the user guide.

If you're not using entities, you'll have to do that yourself. You can always check with is_numeric, but PHP will automatically typecast from string to int if needed.