Help with function in template.php Drupal 7.

Can anyone spot what could be wrong with this function?  I'm just trying to pull a taxonomy term or its parent.  The taxonomy is called "pubissue."

In template.php:

 function ain_core_getpubname() {

// Load the node from unaliased url
if ((arg(0) == 'node') &amp;&amp; is_numeric(arg(1))) {
$node = node_load(arg(1));}


// first find out if it's level two or not
$query = "SELECT t1.tid, t1.parent FROM taxonomy_term_hierarchy t1 JOIN field_revision_field_pubissue t2 ON t1.tid = t2.field_pubissue_tid WHERE t2.entity_id = ".$node-&gt;nid;

    $result = mysql_query($query);
    print_r(mysql_fetch_assoc($result));

        $mytid = $row["tid"];
        $myparent = $row["parent"];

    if ($myparent == 0) {

        // No parent, top level
        $query = "SELECT t1.name FROM taxonomy_term_data t1 WHERE tid = ".$mytid;

        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
            $pubname = $row["name"];        
    }

    else {

        $query = "SELECT t1.name FROM taxonomy_term_data t1 WHERE tid = ".$myparent;

        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
            $pubname = $row["name"];

        }

print $pubname;


}

Then in my node.tpl.php file:

 &lt;!-- Begin Publication --&gt;
    &lt;div class="&lt;?php print $node-&gt;type; ?&gt;-pubname"&gt;&lt;?php ain_core_getpubname() ?&gt;&lt;/div&gt;
&lt;!-- End Publication --&gt;


OK, so going with the suggestion below:

function ain_core_getpubname() {

// Load the node from unaliased url
if ((arg(0) == 'node') &amp;&amp; is_numeric(arg(1))) {
$node = node_load(arg(1));}


// first find out if it's level two or not
$query = "SELECT t1.tid, t1.parent FROM taxonomy_term_hierarchy t1 JOIN field_revision_field_pubissue t2 ON t1.tid = t2.field_pubissue_tid WHERE t2.entity_id = ".$node-&gt;nid;

    $result = db_query($query);
    $row = db_fetch_array($result);

        $mytid = $row-&gt;tid;
        $myparent = $row-&gt;parent;

    if ($myparent == 0) {

        // No parent, top level
        $pubname = db_query("SELECT t1.name FROM taxonomy_term_data t1 WHERE tid = ".$mytid)-&gt;fetchField();
    }

    else {

        $pubname = db_query("SELECT t1.name FROM taxonomy_term_data t1 WHERE tid = ".$myparent)-&gt;fetchField();

        }

print $pubname;


Still not working.  Will continue to troubleshoot, I think I am on the right track.  Thanks again.
Your queries should look something like this:

$query = "SELECT t1.tid, t1.parent FROM {taxonomy_term_hierarchy} t1 JOIN {field_revision_field_pubissue} t2 ON t1.tid = t2.field_pubissue_tid WHERE t2.entity_id = :nid";
$result = db_query($query,array(':nid' =&gt; $node-&gt;nid));
// Result is returned as a iterable object that returns a stdClass object on each iteration (@see http://api.drupal.org/api/drupal/includes--database--database.inc/function/db_query/7#comment-7019)


After all I would suggest you check out the drupal taxonomy api. Using something like taxonomy_get_tree() could be much better.

EDIT:

Using the taxonomy api I would say what you want to do would be something like:

$parents = taxonomy_get_parents($tid);
if(!empty($parents)) {
    // use whatever you need from your $parents
}
else {
    $term = taxonomy_term_load($tid);
    // use whatever you need from the term (No parent, top level)
}


EDIT 2: fixed code example...