Jump to content

is_* problem.


mike93H

Recommended Posts

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:00
String: Fld2 - 111202
String: Fld3 - 80988
Float:  Fld4 - 320.15597198865
Int:    Fld5 - 1500
Float:  Fld6 - 0
Float:  Fld7 - 1500
Float:  Fld8 - 1500
String: Fld9 - 11
String: Fld10 - 3986
Int:    Fld11 - 350
NULL:   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

Thank you, you've solved it. :happy-04:

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.