Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,492
    Plugin Contributions
    88

    Default Using exactly-equal comparisons on database fields

    There's been a push recently to use exactly-equal (===) comparisons to 'tighten' various sections of the code. Be careful when applying those comparisons to information retrieved via database queries.

    I'd thought that fields defined as integer/float values would be returned as their respective value types, but all field values other than NULL are being returned as strings.

    Running zc156c with the demo products, you can view those results via a teeny test script:
    Code:
    <?php
    require 'includes/application_top.php';
    
    $check = $db->Execute(
        "SELECT * FROM " . TABLE_PRODUCTS . " WHERE products_id = 221 LIMIT 1"
    );
    echo nl2br(var_export($check->fields, true)) . '<br><br>';
    
    $test = array(1, 2, 3);
    echo nl2br(var_export($test, true));
    
    require DIR_WS_INCLUDES . 'application_bottom.php';
    ... which produces the following output, showing that (for instance) the products_id (defined as an integer value) is being returned as a string value:
    Code:
    array (
    'products_id' => '221',
    'products_type' => '1',
    'products_quantity' => '1000',
    'products_model' => 'DOWNLOAD1',
    'products_image' => '1_small.jpg',
    'products_price' => '39.0000',
    'products_virtual' => '0',
    'products_date_added' => '2019-05-14 15:22:16',
    'products_last_modified' => NULL,
    'products_date_available' => NULL,
    'products_weight' => '0',
    'products_status' => '0',
    'products_tax_class_id' => '1',
    'manufacturers_id' => '0',
    'products_ordered' => '0',
    'products_quantity_order_min' => '1',
    'products_quantity_order_units' => '1',
    'products_priced_by_attribute' => '0',
    'product_is_free' => '0',
    'product_is_call' => '0',
    'products_quantity_mixed' => '1',
    'product_is_always_free_shipping' => '0',
    'products_qty_box_status' => '1',
    'products_quantity_order_max' => '0',
    'products_sort_order' => '0',
    'products_discount_type' => '0',
    'products_discount_type_from' => '0',
    'products_price_sorter' => '39.0000',
    'master_categories_id' => '60',
    'products_mixed_discount_quantity' => '1',
    'metatags_title_status' => '0',
    'metatags_products_name_status' => '0',
    'metatags_model_status' => '0',
    'metatags_price_status' => '0',
    'metatags_title_tagline_status' => '0',
    'posm_manages_price' => '0',
    )
    
    array (
    0 => 1,
    1 => 2,
    2 => 3,
    )

  2. #2
    Join Date
    Dec 2007
    Location
    Payson, AZ
    Posts
    1,076
    Plugin Contributions
    15

    Default Re: Using exactly-equal comparisons on database fields

    Quote Originally Posted by lat9 View Post
    There's been a push recently to use exactly-equal (===) comparisons to 'tighten' various sections of the code. Be careful when applying those comparisons to information retrieved via database queries.

    I'd thought that fields defined as integer/float values would be returned as their respective value types, but all field values other than NULL are being returned as strings.

    Running zc156c with the demo products, you can view those results via a teeny test script:
    It's a string representative of the cell value returned. Stackoverflow comments.. explains it better.. and how to handle it.
    Dave
    Always forward thinking... Lost my mind!

  3. #3
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,492
    Plugin Contributions
    88

    Default Re: Using exactly-equal comparisons on database fields

    Quote Originally Posted by davewest View Post
    It's a string representative of the cell value returned. Stackoverflow comments.. explains it better.. and how to handle it.
    That's my point, the integer/float values are returned as strings, so you have to be careful applying the exactly-equal-to comparisons, since 0 !== '0'.

 

 

Similar Threads

  1. v139h How exactly one transfers from one database to another.
    By lruskauff in forum Upgrading to 1.5.x
    Replies: 1
    Last Post: 12 Jan 2014, 07:12 AM
  2. Winmerge comparisons and whitespace?
    By countingsheep in forum General Questions
    Replies: 2
    Last Post: 12 Jul 2010, 06:48 PM
  3. Price Comparisons
    By jford in forum Setting Up Categories, Products, Attributes
    Replies: 0
    Last Post: 10 Sep 2007, 04:46 PM
  4. Exactly how do I back Up a MySql database?
    By larryhyman in forum Installing on a Linux/Unix Server
    Replies: 5
    Last Post: 17 Apr 2007, 03:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR