I’m trying to understand MySQL Stored Procedures, I want to check if a users login credentials are valid and if so, update the users online status:
-- DROP PROCEDURE IF EXISTS checkUser;
DELIMITER //
CREATE PROCEDURE checkUser(IN in_email VARCHAR(80), IN in_password VARCHAR(50))
BEGIN
SELECT id, name FROM users WHERE email = in_email AND password = in_password LIMIT 1;
-- If result is 1, UPDATE users SET online = 1 WHERE id = "result_id";
END //
DELIMITER ;
How Can I make this if-statement based on the resultsets number of rows == 1 or id IS NOT NULL?
NB: I have not tested this. It’s possible that MySQL doesn’t like UPDATE against a table it currently has a cursor open for.
PS: You should reconsider how you’re storing passwords.
Re comment about
RETURNvs.OUTvs. result set:RETURNis used only in stored functions, not stored procedures. Stored functions are used when you want to call the routine within another SQL expression.You can use an
OUTparameter, but you have to declare a user variable first to pass as that parameter. And then you have to select that user variable to get its value anyway.When returning result sets from a stored procedure, it’s easiest to use a
SELECTquery.