If this is your first visit, check our documentation. To post, register or login.
Forums / Code Collaboration / Using exactly-equal comparisons on database fields

Using exactly-equal comparisons on database fields

Started by lat9 on 7 Dec 2019, 2:28 PM · Views: 1,671

Results 1 to 3 of 3
7 Dec 2019, 2:28 PM
#1
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

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:

<?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:

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,
)
7 Dec 2019, 3:45 PM
#2
davewest avatar

davewest

Totally Zenned

Join Date:
Dec 2007
Location:
Payson, AZ
Posts:
1,075
Plugin Contributions:
7

Re: Using exactly-equal comparisons on database fields

lat9:

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!

7 Dec 2019, 4:10 PM
#3
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: Using exactly-equal comparisons on database fields

davewest:

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'.