7

There's a really simple query related to the enabled and working Ubercart, where the task is to get all the nids from uc_products table, which simply doesn't give any results:

$query = 'SELECT nid FROM uc_products';
$result = db_query($query);
dsm('===================================================');
dsm($query);
while ($nid = db_result($result)) {
    dsm('$nid: '.$nid);
}

BUT when I run the very same query in the same database in phpMyAdmin, it works, and it gives me ALL the correct results (I have 8 products now).

phpMyAdmin screenshot

BUT the following queries work correctly, and give me all the expected results (the first is the strangest, because vid field is of the same type, but only has another name):

$query = 'SELECT vid FROM uc_products';
$result = db_query($query);
dsm('===================================================');
dsm($query);
while ($vid = db_result($result)) {
    dsm('$vid: '.$vid);
}

$query = 'SELECT * FROM uc_products';
$result = db_query($query);
dsm('===================================================');
dsm($query);
while ($row = db_fetch_object($result)) {
    dsm('$row->nid: '.$row->nid);
}

$query = 'SELECT nid FROM node';
$result = db_query($query);
dsm('===================================================');
dsm($query);
while ($nid = db_result($result)) {
    dsm('$nid: '.$nid);
}

Here's a screenshot of the test results:

screenshot of the tests

As you can see, the first two queries don't return any results, while the others work correctly.
This is a really strange problem. I didn't modify the core modules, so I don't understand the situation. It doesn't matter whether I run it on localhost or my ongoing public server, the result is the same. This means that uc_order_condition_has_products_form() in Conditional Actions menu doesn't work correctly either. I've never ever met such problems in Drupal.

Does anyone have a clue where I should start debugging?

9
  • 3
    Just FYI, your code works perfectly for me on 3 Drupal 6 Ubercart sites (I've just copy/pasted your code into '/devel/php/' and all of the nids were outputted in the messages area as expected) so it's definitely specific to your installation. Very strange
    – Clive
    Commented Jun 29, 2012 at 17:00
  • @Clive: thanks for your answer, yes, you're absolutely right, I forgot to tell that I also tested it in another "clear" Drupal 6.22 (the same "old" version) which I use for just testing, and there it works absolutely fine. But I can't even imagine what kind of problem there could be in this version of Drupal, because - as far as I remember - I didn't modify Drupal's core modules. So I simply don't understand. I should update my Drupal, but it still doesn't explain why node ids work, uc_products ids don't. But maybe an update could remove any kind of messy stuffs, I don't have a clue...
    – Sk8erPeter
    Commented Jun 29, 2012 at 18:00
  • @Clive: please see my updated question... SELECT * FROM uc_products works perfectly as it should... :-O The previously mentioned SELECT nid FROM uc_products still doesn't.
    – Sk8erPeter
    Commented Jun 29, 2012 at 18:25
  • @Clive: FYI, SELECT vid FROM uc_products also works correctly too! Why can Drupal have such a problem with the nid field?
    – Sk8erPeter
    Commented Jul 2, 2012 at 0:42
  • 3
    turn on the devel module and enable the qury logging at admin/settings/devel - that will show what queries are being run on the page. Maybe that query is getting modified somehow along the way?
    – greggles
    Commented Aug 14, 2012 at 12:56

1 Answer 1

2

I think it is likely because your first result is a "0" and php will evaluate that as a False, which stops your while loop from running.

1
  • Wow, it was a long time ago... I can't test that site anymore, but you're right! :D I think I didn't notice back then that the first nid's value was really equal to 0, which shouldn't happen, so it's unexpected in my opinion (0 as a node id doesn't really make sense, it should start from 1) - but it really can stop the while loop immediately. Thanks! +1 and accepted. :)
    – Sk8erPeter
    Commented Jun 15, 2015 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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