Yes, that is possible. It doesn't look like the code in PHP will slow your script down that much though. In my experience (working with DBs and PHP), PHP is always faster to execute conditional statements than using the database to execute these statements.
Here is a guide that will help you write your stored procedure though:
MySQL :: MySQL 5.0 Reference Manual :: 18 Stored Procedures and Functions