views:

108

answers:

2

Why does some query that works in mysql doesn't work with db_query? For instance -

" SELECT * FROM {tb1},{tb2}WHERE {tb1}.vid=%d " (Of course I substitute %d with actual vid value while I'm testing in mysql environment)

"SELECT f1,f2,...,f10 FROM {tb1} INNER JOIN {tb2} ON {tb1}.vid = {tb2}.vid WHERE {tb1}.vid = %d AND {tb2}.vid = %d

Although I get 1 record returned in mysql environment for both statements, db_query doesn't return anything at all. Any idea what mistake I'm making? For what is worth, I'm using IIS 7.5, Mysql 5.5, php 5.2.12 *UPDATE* db_query/db_fetch_object work just fine. It just that hook_view isn't being invoke. So, as a result, no data is displayed. Sorry for the trouble.

UPDATED

function mymodule_load($node){
   $query = 'SELECT f1,f2,...,f10 FROM {tb1} INNER JOIN {tb2} ON {tb1}.vid = {tb2}.vid WHERE {tb1}.vid = %d AND {tb2}.vid = %d';

   $result = db_query($query,$node->vid);//If I use db_query($query,$node->vid,$node->vid), drupal doesn't invoke hook_view

   drupal_set_message($node->vid,"status"); //for testing purpose
   return db_fetch_object($result);
}

function mymodule_view($node, $teaser = FALSE, $page = FALSE){
    $node = node_prepare($node, $teaser); // get it ready for display

    $f1 = check_markup($node->f1);
     ..............
    $f10 = check_markup($node->f10);

    // Add theme stuff here
    $node->content['mycontent'] = array(
       '#value' => theme('defaultskin', $f1,...,$f10),
       '#weight' => 1,
    );


    return $node;
}

function mymodule_theme(){

    return array(
        'defaultskin' => array(
            'template' => 'node-defaultskin',
            'arguments' => array(               
                'f1' => NULL,
                ......
                'f10' => NULL,
            ),
        ),
    );
}
+1  A: 

Have you tried...

db_query("SELECT f1,f2,...,f10 FROM {tb1} tb1
INNER JOIN {tb2} tb2 ON tb1.vid = tb2.vid
WHERE tb1.vid = %d AND tb2.vid = %d", $vid, $vid);

kidrobot
I've tried that. It still doesn't work. The strange thing is, if I provide WHERE clause with two conditions such as above with only one placeholder argument in db_query, drupal invoke hook_view, although the actual data are not filled in $node object;just empties. When I pass two placeholder arguments just to correspond the number of placeholder in sql statement, drupal no longer invoke hook_view. It happens same for one WHERE clause condition with one placeholder argument. I've updated my post so that you have clear picture of what I'm saying.
Andrew
A: 

Your implementation of hook_load is a bit wrong.

/**
 * Implementation of hook_load().
 */
function module_load($node) {
  $additions = db_fetch_object(db_query('f1,f2,...,f10 FROM {tb1} 
                                         INNER JOIN {tb2} ON {tb1}.vid = {tb2}.vid
                                         WHERE {tb1}.vid = %d', $node->vid)); //

  return $additions;
}

You shouldn't return the node, but the additions you want to add to the node. Also you don't need two where clauses since you join on the vid.

Your problem is probably that the query is working, but that you are returning the wrong thing in the hook.

You can also see an example of it's use in the example module.

googletorp
I'm using db_fetch_object to get the actual data.
Andrew
Hi google, thanks for your reply. My hook_load doesn't return $node. It returns object return from db_fetch_object. I have mentioned my weird problem with "WHERE" clause in comment response to Kidrobot reply.
Andrew
I retract my above comment. I tested and found db_query/db_fetch_object isn't at fault. They work just fine. My conclusion of no result came from the fact that hook_view wasn't being invoked.
Andrew