What you are trying to do is harder than it may first appear.
To put some code behind LaserLight's good recommendations
SELECT COUNT(*)
FROM mytable
WHERE id<$targetid
For the 20-above/20-below scenario, I think you'll have trouble doing this in 1 query.
Best would be to combine the array of results from 2 queries. USE ORDER BY and LIMIT to get 20 rows above and below the targe:
For 20 less than or = to target:
SELECT * FROM mytable WHERE id<=$targetid
ORDER BY ID DESC
LIMIT 20
For 20 more than target
SELECT * FROM mytable WHERE id>$targetid
ORDER BY ID
LIMIT 20
Take the results of the 2 returned arrays and sort them using the [man]sort[/man] function.