I am trying to get the user ID associated with a node using this code in Drupal 7, without any luck:

$uid =  $node->uid;
echo "uid: " . $uid;

$uid doesn't have any value in it.

I want to get it so that I can count the number of my members' posts.

$node = node_load('NID');
$uid = $node->uid;
$sql = "SELECT nid FROM {node} WHERE uid = $uid";
$result = db_query($sql);

$number_of_rows = $result->rowCount();
echo "uid: " . $uid;
print "<p>$node->name has submitted <em><strong>$number_of_rows</strong></em> source code / articles.</p>";'

I tried putting it in a display suite module under fields. I also tried putting in a page content type.

  • 1
    Where are you doing this? In the theme? In a module? In a block? Why do you need the uid?
    – paul-m
    Commented Feb 25, 2012 at 7:14

5 Answers 5


for Drupal 8 you can use

$uid = $node->getOwnerId();

That should work. Try making sure that you have a properly loaded node object.

$node = node_load('NID');
echo $node->uid;

It is possible that if the node was created by an anonymous user that the $uid would be zero and thus wouldn't print.

  • I tried your code but still there is no output when I run it. What I am missing now? The node was created by registered user.
    – jaypabs
    Commented Feb 25, 2012 at 6:22
  • 1
    @jaypabs - Are you sure that you actually run the above code? You would have to replace the NID parameter with the actual nid of the node you would like to load. Commented Feb 25, 2012 at 9:05
  • Even a zero as $uid should be printed. I suspect that node_load returns FALSE (because 'NID' was passed as an argument instead of the actual nid of the node (or a node with the specified nid does not exist)).
    – Oswald
    Commented Mar 24, 2012 at 19:23
  • @KristerAndersson How do I get the nid? I thought I just put the code as is "$node = node_load('NID');" I am using a token and by using this code "$nid = '[node:nid]';" I can't still get the nid.
    – jaypabs
    Commented Mar 25, 2012 at 5:38
  • 'NID' is the "node id" that you are trying to load. Each node has a corresponding nid associated with it. You can view a node's nid by looking at the url when you edit a node: example.com/node/NID/edit . The NID is a numeric value from 1 to however many nodes you have.
    – acouch
    Commented Mar 25, 2012 at 6:50

I think you need with this message print "

$node->name has submitted $number_of_rows source code / articles.


To print the number of nodes by each user after submit(add) a new node You have to add drupal_set_message in hook_node_save using this sql

 global $user;
 $sql = "SELECT count(nid) FROM {node} WHERE uid = $user->uid";

Use Views. See this other question for some clarity.

If you want to attach this information to a node, you can use Viewfield.

Also: There's a better query you could use, instead of SELECTing all the content and then counting the results. You could use COUNT and let the DB server do it. COUNT nid FROM {node} WHERE uid = $uid This is essentially what Views is doing for you.

  • 1
    There are many things one can do with views for, but this should not be one of them. The only thing missing in his code example for it to work is an actual NID, I'm surprised no one has mentionned this and just keep reposting his flawed code example. Commented Aug 15, 2012 at 1:08

You can only get the author id of a node by using the following code. FYI it will work only in the node template.

<?php echo $uid; ?>

If you want to use the user id in any other template or in any custom module you can use the following code.

<?php $node = node_load(NID); $uid = $node->uid;?>

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.