Get a Taxonomy Term by Name & Vocabulary in Drupal 6

While developing a site with a master taxonomy driving the navigation, I found a need to return taxonomy data specific to the master taxonomy. Unfortunately, Drupal 6 does not have a native function to do so.

function taxonomy_get_term_by_name_and_vocabulary($name, $vid) {
  $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND t.vid = %d", 't', 'tid' ), trim($name), $vid);
  $result = array();
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }
  return $result[0];
}

In my use-case, my terms were specific enough that I would never return more than one match. If you do have this possibility, change the return value to this:

return $result;