I just wrote a quick hack to get the taxonomy-id based on the node that the user is on from table 'term_node'. for now I hard-coded the username/pwd and so on into my function to pass to the mysql_connect() statement.

I was wondering if there is perhaps a more efficient and secure way to do this ?

please advise
thanks!
- jochen

Comments

jochenh’s picture

anybody on this one ?
???
thanks
- jochen

DriesK’s picture

I think you should provide some more information: where do you want to use it (module, custom page,...), what do want to do with it, ...?

Generally speaking: never use mysql_* functions, but use the Drupal db abstraction layer functions (db_*). You also shouldn't connect to the db, Drupal does that for you. So to get the tid of a node, you could do:

$tid = db_result(db_query('SELECT tid FROM {term_node} WHERE nid = %d', $nid));

(the use of {} and %d are explained on the page I referred to above)

However, remember that more than 1 term can be associated with a node, so better is to use something like:

$result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $nid);
while ($tid = db_result($result)) {
 //do something with $tid;
}

Furthermore, the taxonomy_* functions (you can search for them in drupaldocs) may be of use to you. If you want more than just the tid, you could use:

$terms = taxonomy_node_get_terms($nid);

This will result in an array, the keys being the tids of the terms associated with the node, and the values being an object containing all information about that term (tid, vid, name, description, weight). See here for more information on this function.

jochenh’s picture

thanks !
this one is all i needed.

$tid = db_result(db_query('SELECT tid FROM {term_node} WHERE nid = %d', $nid));
DriesK’s picture

Oh, and to get the nid of the current page, see here.