Best way to get current user purchased products
One one thing that I can think of to change. Make sure you only get a single result.

$query = db_select('commerce_order', 'corder');
$query-&gt;join('commerce_line_item', 'li', 'corder.order_id = li.order_id');
$query-&gt;join('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
$query-&gt;condition('corder.uid', $user-&gt;uid, '=')
      -&gt;condition('corder.status', 'completed', '=')
      -&gt;addExpression('DISTINCT prod.commerce_product_product_id', 'product_id');


Using this line:

-&gt;addExpression('DISTINCT prod.commerce_product_product_id', 'product_id');


instead of:

-&gt;fields('prod', array('commerce_product_product_id'));


will insure that you only get one product_id per row in your result. By using the DISTINCT SQL Keyword, it will only allow distinct values to be returned.