I don't know PHP but I'll assume your code is functional, looks like it would be.
If by "search engine friendly" you mean "Will the spiders follow the link?", then it depends on what your link looks like, but if it's something like <A href="clicks.php?adnumber=5>The Ad Text</A> then yes, the spiders will follow it.
If by "search engine friendly" you mean that you want the link to pass PR to the destination, then no, it won't. I don't know a way to track outbound clicks from your site and still have a clean, direct link to the destination. Maybe there's such a thing and I just don't know about it, though.