there are a few reasons.
the first reason is mostly for reasons of separation of your code structures. Many believe that manipulation of data should be handled by the database itself, and as such, SPs are needed.
Are you saying that the select and results of same can be kept in the db?
nope. All i meant is that the actual manipulation of the data, such as looping through records, inserts, updates, edits, should be handled by the database technology itself. Part of the reason is so that you know that all your database tinkering is happening with code that is native to the database. Otherwise, if PHP for example, was to change the way the mysql_query works, you might get funky result, or even fatal, if you are not keeping on top of functionality changes in the PHP processor. Now that is unlikely to happen, but it has with probably every language out there. This is the reason why you actually do care about your next quote.
Another reason, related to the separation, is that it can make your application code, database agnostic. Although the SPs for msSQL and mySQL are different, the input and output from those can be exactly the same. therefore, you application could give a crap about which database is being used (for the most part).
Interesting, but prolly doesn't apply to my case.
Good practice though. Your projects today may just be practice for your projects tomorrow. realize too that it also makes it code agnostic. since the same db could be talked to by any application that knows the SPs.
SPs are faster than sending the query from PHP/ASP/etc. This is because the SP is compiled/cached, and so are the queries associated with it, or at least they are supposed to be.
Now we're getting somewhere. So, the php interpreter doesn't need to read and react each time the same query is served? With 20k or so pages that becomes a factor to reckon with.
what TD said, but I think you understood what I meant. The results are not cached without some kind of caching engine, which i think exists, or you can create. but the actual procedure, and the execution path are cached/compiled. So mySQL doesnt need to parse the query each time it runs. When you do it from PHP, mysql has to "decode" what crazy shit you wrote, EVERY TIME (there is a caveat here, but not my realm). The results of the SP are still retrieved realtime, or at least the same exact way that it would be from PHP.
SPs are more secure (in theory), since you pass in parameters and the parameter gets type checked. At least in msSQL, it escapes quotes, and wraps with quote automatically for you reducing a major vector for injection attacks.
Security. Aha. That's good to know.
Looks like I have some studying to do. Is my example in the first post a good candidate for a change to stored procedures?
What are some other areas where SP's shine?
From a security point, SPs shelter the application from knowing anything about the internals of the database. like an API for a webservice in a lot of respects. You have no idea whats actually going on behind that API, but you dont care because you get what you need. Same with SPs, which also means, you can allow other applications to access your database via the SPs, and they dont need a username, or password, or anything. just the parameters of the SPs.
basically, an SP is probably always good to use. however, if you dont need to squeeze cycles, and you dont care about being Database agnostic, then probably not worth it to convert something. However, if you are currently building it, why not do it in SPs.
One more advantage is in your PHP. most SP calls are going to be much simpler to follow than the jumble of shit you most likely have to build crazy SQL strings. The call is like a magic box, you send in some parameters, and you get back your result. Just like how you currently do it, but in less PHP lines.
The downside to SPs is that your code is now fragmented. You have code to manage in 2 places. This is part of the reason I have not used SPs in alot of my client work. They take it over, and they only know ASP, or SQL scares them, or or or or.