function mysql_escape_array(&$values)
{
    $escaped_values = array();
    foreach ($values as $value) {
        if (is_int($value) || is_float($value)) {
            $escaped_values[] = $value;
        } elseif (is_string($value)) {
            $escaped_values[] = '"' . mysql_escape_string($value) . '"';
        } elseif (is_array($value)) {
            $escaped_values[] = '(' . mysql_escape_array($value) . ')';
        } else {
            die("Don't know how to escape '{$value}'");
        }
    }
    return implode(',', $escaped_values);
}
 function untag_books(&$aliases, &$tags)
 {
     if (!count($tags) || !count($aliases)) {
         return 0;
     }
     $result = mysql_query("SELECT id " . "FROM tag " . "WHERE tag in (" . mysql_escape_array($tags) . ")") or die(__FILE__ . ':' . __LINE__ . ':' . mysql_error());
     $tag_ids = mysql_fetch_fields($result);
     if (!count($tag_ids)) {
         return 0;
     }
     $result = mysql_query("SELECT id " . "FROM alias " . "WHERE alias in (" . mysql_escape_array($aliases) . ")") or die(__FILE__ . ':' . __LINE__ . ':' . mysql_error());
     $alias_ids = mysql_fetch_fields($result);
     if (!count($alias_ids)) {
         return 0;
     }
     mysql_query("DELETE " . "FROM alias_tag " . "WHERE " . "tag_id IN (" . mysql_escape_array($tag_ids) . ") AND " . "alias_id IN (" . mysql_escape_array($alias_ids) . ")") or die(__FILE__ . ':' . __LINE__ . ':' . mysql_error());
     return mysql_affected_rows();
 }