[support] db query to find out if member has node of type X

Jason Flatt drupal at oadaeh.net
Fri Oct 5 13:34:09 UTC 2007


On Friday 05 October 2007 03:02:22 Neil: esl-lounge.com wrote:
> How would I find out if User A had authored a node of type X?
>
> I have a link "View Story" that I would like to be "Create Story" if that
> member hasn't yet created that node type.
>
> Is that a complicated db query to do?

Probably something like this would do it:

global $user;
$node_type = 'story';
if (db_num_rows(db_query("SELECT * FROM {node} WHERE type='%s' AND uid=%d", 
$node_type, $user->uid))) {
  print(t("View story"));
}
else {
  print(t("Create story"));
}

Note: that is totally untested code and is likely to contain one or more 
errors.

-- 
Jason Flatt
http://www.oadaeh.net/
Father of Six:  http://www.flattfamily.com/ (Joseph, 14; Cramer, 12; Travis, 
10; Angela; Harry, 7; and William, 12:04 am, 12-29-2005)
Linux User:     http://www.xubuntu.org/
Drupal Fanatic: http://drupal.org/


More information about the support mailing list