PHP/MySQL: Check if something 'belongs-to' something else?

Posted by Nebs on Stack Overflow See other posts from Stack Overflow or by Nebs
Published on 2010-04-29T21:00:43Z Indexed on 2010/04/29 21:17 UTC
Read the original article Hit count: 228

Filed under:
|
|

I have two tables: Stores and Items. The relationship is: Stores 1---* Items

In PHP/MySQL what would be the best (fastest/simplest) way to check if a particular item belongs to a particular store.

In other words given for example:

$store_id = 1;
$item_id = 12;

I want to check if item 12 belongs to store 1 (and not some other store).

I usually do a select on Items matching both the store_id and item_id and limit the results to 1. Then check how many rows (0 or 1) were returned with mysql_num_rows. Is there a better way?

Update:

Both tables have an "id" column. The Items table has a "store_id" column.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql