How about moving away from these mysql_functions to mysqli or pdo? These functions are deprecated since PHP 5.5.0 and are completely removed in PHP 7.0.0. Otherwise, if you still need to do this, have you tried using an incrementing counter instead of using mysql_num_rows?
function submenu($parentid = 0)
{
$sql = mysql_query("SELECT * FROM test WHERE refid=" . $parentid);
echo '<ul>';
$numRows = mysql_num_rows($sql);
$i = 0;
while($row = mysql_fetch_array($sql))
{
if($numRows > $i)
{
echo '<li>' . $row['name'];
submenu($row['id']);
echo '</li>';
}
else
{
echo '<li><a href="' . $row['userdefined'] . '">' . $row['name'] . '</a>';
submenu($row['id']);
echo '</li>';
}
$i++;
}
echo '</ul>';
}