0
votes

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,
            ),
        ),
    );
}
2

2 Answers

1
votes

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);

0
votes

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.