0

I'm having trouble figuring out why the db_select() is not returning any results. Long story short, if I pass the results of $results->execute() through a foreach ($results as $result), I don't get any results. If I take the generated SQL and go directly to command line or phpMyAdmin (substituting manually for the placeholders) I get the expected result. I feel like I have to just be overlooking something - I'm hoping fresh eyes can point it out for me.

Here is the select:

$results = db_select('product_charges_override', 'pc');
$results->join('products', 'p', 'p.product_id = pc.product_id');
$results->fields('pc', array('uid', 'product_id', 'override_count', 'redemption_count', 'coupon_code'))
  ->fields('p', array('human_name'))
  ->condition('pc.coupon_code', $coupon_code);
$results->execute();

Here is the result of dpq($results):

SELECT pc.uid AS uid, pc.product_id AS product_id, pc.override_count AS override_count, pc.redemption_count AS redemption_count, pc.coupon_code AS coupon_code, p.human_name AS human_name FROM {product_charges_override} pc INNER JOIN {products} p ON p.product_id = pc.product_id WHERE (pc.coupon_code = :db_condition_placeholder_0)

Here is the relevant Where statement from just var_dump($results):

protected 'where' => 
  object(DatabaseCondition)[136]
    protected 'conditions' => 
      array
        '#conjunction' => string 'AND' (length=3)
        0 => 
          array
            'field' => string 'pc.coupon_code' (length=14)
            'value' => string 'ugd2pl8z92j9' (length=12)
            'operator' => string '=' (length=1)
    protected 'arguments' => 
      array
        ':db_condition_placeholder_0' => string 'ugd2pl8z92j9' (length=12)
    protected 'changed' => boolean false
    protected 'queryPlaceholderIdentifier' => string '52e7fdf78bbd25.63959182' (length=23)
    public 'stringVersion' => string ' (pc.coupon_code = :db_condition_placeholder_0) ' (length=48)

And, finally, the query as I run it from the command line (notice the '{' and '}' have been removed, as well as the placeholder replaced with the value from the 'where' object). Those are the only things I replaced from the results of dpq().

SELECT pc.uid AS uid, pc.product_id AS product_id, pc.override_count AS override_count, pc.redemption_count AS redemption_count, p.human_name AS human_name FROM product_charges_override pc INNER JOIN products p ON p.product_id = pc.product_id WHERE (pc.coupon_code = 'ugd2pl8z92j9')

And an export from phpMyAdmin of the query results:

/**
 * Export to PHP Array plugin for PHPMyAdmin
 * @author Geoffray Warnants
 * @version 0.2b
 */

 //
 // Database "mats_theme"
 //

 // mats_theme.product_charges_override
 $product_charges_override = array(
       array('uid'=>null,'product_id'=>7,'override_count'=>5,'redemption_count'=>0,'human_name'=>'Fleet Forum Registration')
);
0

I'm not assigning $results->execute() to anything - that's why I can't iterate over the results. Answer was provided by a local mailing list that I sent this link to.

So, solution is $results = $results->execute(). Of course, the query should be renamed to something like, $query;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.