0

I have build a little mysq function who should check is the users in my table are still vaid, the have got a row validfrom and validto and my function should check if the validto date is older than now.

CREATE DEFINER=`root`@`localhost` FUNCTION `is_valid_user`(username 
VARCHAR(10)) RETURNS tinyint(1)
BEGIN
DECLARE valid_from, valid_to datetime;
SELECT
Gültig_von, Gültig_bis
INTO valid_from , valid_to FROM
Benutzer
WHERE
Benutzername = username;

IF valid_from <= now() and valid_to > now() THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END if;

END

if i want to proceed this function i get the error message: Result consisted of more than one row.

1 Answers1

0

As per your posted error message your query return more than one rows so just add "LIMIT 1" after where clause so you will have only one row at each time if it exists.

Kishan
  • 773
  • 4
  • 10
  • If i add "Limit 1" every row hast de same value than the first row –  Nov 26 '17 at 12:35
  • At any one time you have to check for any one user so you have to pass limit in your query. Otherwise use column which is unique in your table so you don't have to pass limit also. – Kishan Nov 26 '17 at 12:45
  • I think best way is use your primary key of table as input of your function and just change your where clause like "WHERE id = INPUT_ID_NAME" – Kishan Nov 26 '17 at 13:06