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

Jason Flatt drupal at oadaeh.net
Fri Oct 5 14:26:42 UTC 2007


On Friday 05 October 2007 07:08:25 Neil: esl-lounge.com wrote:
> Your code seems to work for a menu change where the uid, I want to be the
> uid of the member who is navigating the site.
>
> I will also have similar links on members' user pages, profile pages,
> etc....so for these, the uid wouldn't change, it would always be the uid of
> that node's author.
>
> I made the following change and it seems to work:
>
> <?php
> global $user;
> $node_type = 'story';
> if (db_num_rows(db_query("SELECT * FROM {node} WHERE type='%s' AND uid=%d",
> $node_type, $node->uid))) {
>   print(t("View Story"));
> }
> else {
>   print(t("Create Story"));
> }
> ?>

Ah, yes.  One or more errors did exist.  I was even looking at the node table 
to make sure there was the uid column and still put in the user stuff.  The 
line with "global $user;" is unnecessary and can be removed.

And, actually, if the node type will never change, you can eliminate the line 
with "$node_type = 'story';" and replace your "if" statement to this:

if (db_num_rows(db_query("SELECT * FROM {node} WHERE type='story' AND uid=%d", 
$node->uid))) {


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