Hello, thanks for reading.

I'm developing a custom module that will let a user add a value and a comment to an event. On a node load operation, I use hook_nodeapi to check the database for the user's info on that particular node, like so:

global $user;
...

...

case 'load':

$object = db_fetch_object(db_query('SELECT value, comments FROM {stamp} WHERE nid = %d AND uid = %d',
$node->nid,$user-uid));

return array('stamp_value' => $object->value, 'stamp_comments'=>$object->comments);

break;

My problem is that the select query above is not properly grabbing the uid. I print the value $user->uid as soon as we enter the function, and it is 0, as no one is logged in. Then the query runs and the value all of the sudden is 1. I put a print in the db_query just to make sure, and sure enough, the query looks like this.

SELECT value, comments FROM stamp WHERE nid = 2 AND uid=1

Any help or guidance would be appreciated. I'm cracking into the internals of the replace function, but I thought I'd throw this up here in case it is a common bug and I've missed the forum post.

Comments

nevets’s picture

One thing I notice is $user-uid should be $user->uid.

If that is not the problem posting the whole function would help (please place the code between <code> and </code> tags) as it is hard to guess what you are doing between function entry and the line of code you are showing.

toby_panzer’s picture

Thanks nevets.

I was trying to find what was wrong and changing it frequently... hence I cut'n'paste that bad form of the var. I've tried it with $user->uid and also $node->uid now, same result. Here is the _nodeapi hook.


function stamp_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
    
  switch ($op) {
    case 'validate':
    case 'load':
    
      $object = db_fetch_object(db_query("SELECT value, comments FROM {stamp} WHERE uid = '%d' AND nid = '%d'",$user->uid,$node->nid));
      
      return array('stamp_value' => $object->value, 'stamp_comments'=>$object->comments);
      
      break;

    case 'view':
      if (isset($node->stamp_value)){
        $node->content['stamp'] = array(
          '#value' => theme('stamp_event', $node->stamp_value,(isset($node->stamp_comments)?$node->stamp_content:"")),
          '#weight' => 10
        );
      }
      else{
        $node->content['stamp'] = array(
          '#value' => drupal_get_form('stamp_add_form'),
          '#weight' => 10
        );
      
      }
      break;
  }
  
}
toby_panzer’s picture

thanks. Feeling pretty foolish now.