1

好的,这是我的mysql函数

DELIMITER $$

CREATE DEFINER=`root`@`%` FUNCTION `password_reset_request`(spUsername VARCHAR(120)) RETURNS varchar(255) CHARSET utf8
BEGIN
    DECLARE vcode VARCHAR(255);
    DECLARE muid INTEGER;
    SET vcode = "test";

    select id into muid from scheme.users where username=@spUsername limit 1;

    RETURN muid;

    IF muid is null THEN
        RETURN 'BADUSER';
    ELSE
        insert into `password_reset`(`uid`,`code`) 
            values ( muid,vcode)
        ON DUPLICATE KEY UPDATE
            `code`=vcode;
        RETURN vcode;
    END IF;
END

我的问题muid是总是这样null。我什至用 替换了所有内容muid@muid这并没有改变结果。

当我运行select id from scheme.users where username=@spUsername limit 1;并替换@spUsername为传递给函数的相同值时,它会为我提供正确的信息。

我还进行RETURN @spUsername了验证该变量是否设置正确,并且确实如此。

有任何想法吗?大概是在做傻事。

表方案

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(120) NOT NULL,
  `email` varchar(200) NOT NULL,
  `password` varchar(200) NOT NULL,
  `vcode` varchar(120) NOT NULL,
  `isverified` bit(1) DEFAULT b'0',
  `verifydate` datetime DEFAULT NULL,
  `date_created` datetime DEFAULT NULL,
  `date_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username_UNIQUE` (`username`)
)

CREATE TABLE `password_reset` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `code` varchar(255) NOT NULL,
  `iscompleted` bit(1) DEFAULT b'0',
  `date_created` datetime DEFAULT NULL,
  `date_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uid_UNIQUE` (`uid`)
)
4

1 回答 1

1

试试这个:

DELIMITER $$

CREATE DEFINER=`root`@`%` FUNCTION `password_reset_request`(_spUsername VARCHAR(120)) RETURNS VARCHAR(255) CHARSET utf8
    READS SQL DATA
BEGIN
    DECLARE _vcode VARCHAR(255);
    DECLARE _muid INT(10) DEFAULT 0;
    SET vcode = "test";

    SELECT id INTO _muid 
    FROM scheme.users 
    WHERE username = _spUsername 
    LIMIT 1;

    IF _muid = 0 THEN
        RETURN 'BADUSER';
    ELSE
        INSERT INTO `password_reset`(`uid`,`code`) 
            VALUES ( _muid, _vcode)
        ON DUPLICATE KEY UPDATE
            `code` = _vcode;
        RETURN _vcode;
    END IF;
END$$

DELIMITER ;
于 2012-12-21T11:11:29.780 回答