This is driving me insane. I have an Items table (PK item_id). Each item belongs to a category and so one of the fields of the table is category_id. Here is the simplified SQL I am using to debug this:
SELECT * FROM Items WHERE category_id = '?'
I am replacing ? with the category ID's manually for debugging purposes, for now. There are 5 items in the table with id's 1, 2, 3, 4, 5. Now here is where it gets weird.
Item 1's category is 17. In phpMyAdmin the above query using 17 as the parameter returns Item 1. PHP does not.
PHP returns all of the other items fine when using their categories: 15, 13, 10, and 14.
If I alter Item 1 in the database to have a category ID of either 15, 13, 10, or 14 it still does not show up in the results within PHP.
If I alter Item 1 in the database to have a different category ID, for example 4, it still does not show up in the results.
If I alter any other item to have a category ID of 17, that item will no longer show up (both that item and Item 1 do not, i.e., there are no results).
So what is going on here? Item 1, and Item 1 only, will never show up no matter what, and other categories will not show up if given a category of 17 but show up otherwise? And it works in phpMyAdmin, but not PHP?
Here is the PHP code to test this:
$query = mysql_query("SELECT * FROM `Items` WHERE `category_id` = '17'");
while ($r = mysql_fetch_assoc($query))
{
echo $r['item_id'];
}
Here is SHOW CREATE TABLE Items:
CREATE TABLE `Items` (
`item_id` int(11) NOT NULL default '0',
`description` varchar(250) default NULL,
`name` varchar(25) default NULL,
`category_id` int(11) default NULL,
PRIMARY KEY (`item_id`),
UNIQUE KEY `item_id` (`item_id`),
KEY `FK_Items_Categories` (`category_id`),
CONSTRAINT `FK_Items_Categories` FOREIGN KEY (`category_id`) REFERENCES `Categories` (`category_id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1
And Categories:
CREATE TABLE `Categories` (
`category_id` int(11) NOT NULL auto_increment,
`parent_id` int(11) default NULL,
`name` varchar(25) default NULL,
PRIMARY KEY (`category_id`),
KEY `FK_Categories_Categories` (`parent_id`),
CONSTRAINT `FK_Categories_Categories` FOREIGN KEY (`parent_id`) REFERENCES `Categories` (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1
Note: I did not create the database, so let me know if there is a problem with this.
`category_id` = 17SHOW CREATE TABLE Items.