Not quite sure what this is exactly used for but just a few friendly notes after a quick glance.
1) I'd recommend looking into PDO or MySQLi at least.
2) You don't need single quotes around integers. (ie: $sheep[1] not $sheep['1'])
3) You could have your script determine if the tvrage
table exists (if not create it)
4) You can extract