Here's one way to shorten your code:
$sql_max_id = 'SELECT max(id) FROM `categories`';
$recordset_max = mysql_query($sql_max_id,$link_id);
$max_id = mysql_result($recordset_max, 0);
Here's an alternate query, which might be more optimized for your MySQL table (depending on structure), and then again it might not be. Using EXPLAIN should tell you (I'm new to MySQL optimization):
$sql_max_id = 'SELECT id FROM `categories` ORDER BY id DESC LIMIT 1';
$recordset_max = mysql_query($sql_max_id,$link_id);
$max_id = mysql_result($recordset_max, 0);