fe_81 Posted June 30, 2011 Share Posted June 30, 2011 Hi, I use PDO MySQL with Zend Framework database adapters. When I select data from the database, it is always returned as strings. From what I have understood, this is the normal behavior. But this creates a lot of unnecessary typecasting code in various places of the application. One example is when data is encoded into JSON and used in javascripts. Assuming that everything are strings won't work since some variables might have types if they were not pulled from the database. Is it possible to have some kind of automatic type hinting for database adapters so that all data comes in the right types from the database, or are there any other good solutions to this problem? Thank you for your help! -F Quote Link to comment https://forums.phpfreaks.com/topic/240791-typecasting-when-selecting-data-from-database/ Share on other sites More sharing options...
fe_81 Posted July 4, 2011 Author Share Posted July 4, 2011 Has anyone else this problem ever or something similar when getting data from the database and JSON-encoding it for some ajax requests. Or is it just something you have to live with (either you have to typecast the data in the php-code or you have to do it javascript (possibly with some additional checks). Quote Link to comment https://forums.phpfreaks.com/topic/240791-typecasting-when-selecting-data-from-database/#findComment-1238149 Share on other sites More sharing options...
trq Posted July 4, 2011 Share Posted July 4, 2011 What exactly is the problem with your JSON? Quote Link to comment https://forums.phpfreaks.com/topic/240791-typecasting-when-selecting-data-from-database/#findComment-1238154 Share on other sites More sharing options...
fe_81 Posted July 4, 2011 Author Share Posted July 4, 2011 The problem is that the variables may come as either strings or numbers. Especially booleans. Check this json string for example: { "booleanFromDB":"true", "booleanFromPHP":true, "integerFromDB":"123", "integerFromPHP":123 } I would like to avoid all such code on the js as: if (someVariable == "true" || someVariable == true) { do something ... } I would like to find a somewhat generic way to send data from MySQL->PHP->JS and back (JS->PHP->MySQL) without having to do lot's of typecasting or checks. Thanks, F Quote Link to comment https://forums.phpfreaks.com/topic/240791-typecasting-when-selecting-data-from-database/#findComment-1238232 Share on other sites More sharing options...
PFMaBiSmAd Posted July 4, 2011 Share Posted July 4, 2011 Assuming that you have fetched a row from the database in $row, the following will handle the conversion of numeric values - <?php function make_int($val){ return is_numeric($val) ? (int)$val : $val ; } $row = array_map('make_int',$row); echo json_encode($row); ?> The above could be extended to handle boolean values. Quote Link to comment https://forums.phpfreaks.com/topic/240791-typecasting-when-selecting-data-from-database/#findComment-1238245 Share on other sites More sharing options...
PFMaBiSmAd Posted July 4, 2011 Share Posted July 4, 2011 The following works for float, integer, string, and boolean - <?php function convert_type($val){ if(is_numeric($val)){ return $val + 0; } if(strtolower($val) == 'false'){ return false; } if(strtolower($val) == 'true'){ return true; } return $val; } $row = array_map('convert_type',$row); echo json_encode($row); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240791-typecasting-when-selecting-data-from-database/#findComment-1238251 Share on other sites More sharing options...
fe_81 Posted July 4, 2011 Author Share Posted July 4, 2011 Thank you for your answer! Does this have a risk that some real strings get casted to boolean or int? For example if we have a field username and someone writes "true" or "five" or something like that? One option that I have been thinking about is to write a short script that can be run from the commandline, which would generate classes from the database scheema that take care of the typecasting for each table. Do you know if there is something like this already for Zend Framework? -F Quote Link to comment https://forums.phpfreaks.com/topic/240791-typecasting-when-selecting-data-from-database/#findComment-1238362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.