Forums / General Questions / Query returns no results if an id value is selected

Query returns no results if an id value is selected

Views: 682

Results 1 to 4 of 4
9 May 2015, 3:43 PM
#1
ecomevo avatar

ecomevo

New Zenner

Join Date:
Dec 2014
Posts:
4
Plugin Contributions:
0

Query returns no results if an id value is selected

I have the following table:

CREATE TABLE IF NOT EXISTS `inventory` (
 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
 `products_id` INT(11) UNSIGNED DEFAULT NULL,
 `options_id` INT(11) UNSIGNED DEFAULT NULL,
 `options_values_id` INT(11) UNSIGNED DEFAULT NULL,
 `sku` VARCHAR(255) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `products` (`products_id`,`options_id`,`options_values_id`),
 UNIQUE KEY `sku` (`sku`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

I'm running the following insert on it:

INSERT INTO inventory (
   products_id,
   options_id,
   options_values_id,
   sku
) VALUES (
   2,
   4,
   5,
   'K12R-1687'
);

After running this, I can execute mysql_insert_id($db->link) and get the new auto-increment ID. However, if I run the following query right after that within Zen Cart 1.5, I get no results ($result->RecordCount() equals zero):

$result = $db->Execute("SELECT id FROM inventory WHERE sku = 'K12R-1687' LIMIT 1");

If I then copy this query and run it through PHPMyAdmin, I get the id value expected.

If I change the query to this, then it returns the results:

$result = $db->Execute("SELECT id, sku FROM inventory WHERE sku = 'K12R-1687' LIMIT 1");

Of if I change it to this, it finds the products_id but is missing the id naturally:

$result = $db->Execute("SELECT products_id FROM inventory WHERE sku = 'K12R-1687' LIMIT 1");

No queries which get only the id work.

What the heck is going on here?

10 May 2015, 6:56 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Query returns no results if an id value is selected

In your SELECT query, have you tried wrapping id in backticks? ie: id

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

12 May 2015, 2:07 AM
#4
ecomevo avatar

ecomevo

New Zenner

Join Date:
Dec 2014
Posts:
4
Plugin Contributions:
0

Re: Query returns no results if an id value is selected

Adding the backticks fixed the issue.