mike93H Posted October 20, 2017 Share Posted October 20, 2017 I'm trying to build a MySQL query by running through this array and using "if( is_..." conditions to put the appropriate conversion characters in sprintf statements - Array( [Fld1] => 2017-09-01 12:15:00 [Fld2] => 111202 [Fld3] => 80988 [Fld4] => 320.15597198865 [Fld5] => 1500 [Fld6] => 0 [Fld7] => 1500 [Fld8] => 1500 [Fld9] => 11 [Fld10] => 3986 [Fld11] => 350 [Fld12] => [Fld13] => [Fld14] => 0) The problem is that the is_... functions aren't recognizing the types correctly, e.g. - String: Fld1 - 2017-09-01 12:15:00String: Fld2 - 111202String: Fld3 - 80988Float: Fld4 - 320.15597198865Int: Fld5 - 1500Float: Fld6 - 0Float: Fld7 - 1500Float: Fld8 - 1500String: Fld9 - 11String: Fld10 - 3986Int: Fld11 - 350NULL: Fld12 -NULL: Fld13 -Int: Fld14 - 0 Here's the PHP code - print_r( $Record ); foreach( $Record as $Field => $Value ) { if( is_int( $Value ) ) echo "Int: $Field - $Value\n"; else if( is_float( $Value ) ) echo "Float: $Field - $Value\n"; else if( is_string( $Value ) ) echo "String: $Field - $Value\n"; else echo "NULL: $Field - $Value\n"; if( is_int( $Value ) ) $Query .= sprintf( "`%s` = %d, ", $Field, $Value ); else if( is_float( $Value ) ) $Query .= sprintf( "`%s` = %.14f, ", $Field, $Value ); else if( is_string( $Value ) ) $Query .= sprintf( "`%s` = '%s', ", $Field, $Value ); } The first set of if...else...if statements are just temporary to try to see what was happening. It's making no sense to me because there doesn't appear to be any difference between the integers being identified as strings and those (correctly) identified as integers. Any ideas anyone? Link to comment Share on other sites More sharing options...
Sepodati Posted October 20, 2017 Share Posted October 20, 2017 Where's the data in the array coming from? Link to comment Share on other sites More sharing options...
mike93H Posted October 20, 2017 Author Share Posted October 20, 2017 Thank you, you've solved it. The data comes from various calculations. The integers incorrectly identified as strings are read directly from a DB table. That gave me the clue I needed. Cast the values to the appropriate type has solved the problem. e.g. $array[fld2] = (int) $database_table_field; Link to comment Share on other sites More sharing options...
benanamen Posted October 20, 2017 Share Posted October 20, 2017 Are you really sequentially numbering your DB fields? That is a red flag to a bad DB design, plus the names are completely non-descriptive. Link to comment Share on other sites More sharing options...
mike93H Posted October 21, 2017 Author Share Posted October 21, 2017 No, the DB fields are descriptive in the real system. I just changed the names for this example. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.