You need to create the rule such where all requests are directed to a external script.
Then the server script may look up the database, set cookies and redirect. The .htaccess can not do this by itself unless you write a extension for apache which is likely to be beyond the scope the application you need right now.
For now just redirect all requests to a script by
RewriteRule ^([A-Za-z0-9-])$ /advert.php?id=$1 [L]
you can use $ad_string = $_GET["id"]; to get the ad string in the $ad_string variable. Then you can make a connection to the database using mysql_connect() and then run a sql query like "SELECT * FROM advert_table WHERE id = '" . mysql_real_escape_string($ad_string) . "'" This should sort out your url for the query. then use a meta redirect or a javascript redirect or even a header 301 redirect.