5

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 Feb 25 '12 at 7:14
5

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.

| improve this answer | |
  • 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 Feb 25 '12 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. – Cyclonecode Feb 25 '12 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 Mar 24 '12 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 Mar 25 '12 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 Mar 25 '12 at 6:50
1

in Drupal 8 you can do

$uid = $node->getOwnerId();
| improve this answer | |
0

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";
| improve this answer | |
-1

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.

| improve this answer | |
  • 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. – stefgosselin Aug 15 '12 at 1:08
-2

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;?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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