At 8:49 -0500 9/8/06, Dan Buettner wrote:
Chris, I'm not aware of a way to use "ordinary" SQL (insert, update)
for this, but the use of a stored procedure would work for you.  I've
not done it with MySQL (never had a need) but did things like this
extensively with Sybase.

In rough terms:

CREATE PROCEDURE sp_chances(IN xxx INT) RETURNS INT
BEGIN
   UPDATE table SET chances = IF(chances > 0, chances - 1, 0) WHERE id = xxx;
   SELECT chances FROM table WHERE id = xxx;
END;

Then you would execute  this SQL:
CALL sp_chances(xxx)
and it should return the number of chances left for user id xxx,
having decremented the counter as well (if > 0).

Hi Dan

Thanks for this, but it's clear to me that all the stored procedure is doing is running the two queries I was running otherwise. It would save me a couple of lines of PHP code, but is it really any more efficient? Especially as this will be on a /very/ small database (at least, by the standards of some of the people on this list!) and won't happen particularly often. It'll only get called of the user doesn't get the password right first time, which most of them will do - and there won't be many anyway, at least not at first.

What I was really hoping for was some equivalent of mysql_insert_id(), but returning some other value from the last query.

Not to worry - two quick queries it is... or maybe it would do me good to start learning about stored procedures. :-)

Thanks again!

--
Cheers... Chris
Highway 57 Web Development -- http://highway57.co.uk/

10 percent of computer users are Mac users, but remember,
we are the top 10 percent.
   -- Douglas Adams

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to