You're missing a single step. Try this:
$resource = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
$tagID = mysql_fetch_assoc($resource);
print_r($tag_id);
If your query returns more than one row (i.e. there is more than one tag with the same tagName), you'll want to put it in a loop:
$resource = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
while($tagID = mysql_fetch_assoc($resource)) {
echo $tagID['tagID'];
}
Addendum
Although the above code will solve your problem, I urge you to stop right there and learn about mysqli instead. It's a much newer, more robust solution than using the mysql_* functions. From the docs:
The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later.
The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:
- Object-oriented interface
- Support for Prepared Statements
- Support for Multiple Statements
- Support for Transactions
- Enhanced debugging capabilities
- Embedded server support
Also from the docs:
If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use [the MySQLi] extension.