从php里面的MySQL函数中检索返回值

时间:2012-05-24 17:34:00

标签: php mysql function integer

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

我有以下MySQL功能:

DELIMITER $$
DROP FUNCTION IF EXISTS DoUsernameAndPasswordMatch$$
CREATE FUNCTION DoUsernameAndPasswordMatch(
    UserLogin VARCHAR(20),
    UserPass VARCHAR(20)
)
RETURNS BOOLEAN
BEGIN
DECLARE userCount BOOLEAN;

    SELECT COUNT(*) INTO userCount
    FROM Users_Login 
    WHERE Users_Login.UserLogin = UserLogin AND Users_Login.UserPass = SHA1(UserPass);

    RETURN userCount;    
END$$
DELIMITER ;

我从PHP调用它:

$username_escaped = escapeMySQLParam($_POST["user"]);
$userpass_escaped = escapeMySQLParam($_POST["pass"]);

$mysql_query = "SELECT DoUsernameAndPasswordMatch('$username_escaped', '$userpass_escaped')";
$mysql_results = mysql_query($mysql_query);
$mysql_row = mysql_fetch_row($mysql_results);

但我收到以下错误:

Warning: mysql_fetch_row() expects parameter 1 to be resource, 
boolean given in D:\xampp\webdesktop\login.php on line 22

我理解错误并尝试修复它并解决它没有成功。 是否可以从PHP中检索MySQL的INTEGER值以及如何?

或者你可以告诉我更好的方法来做到这一点。

感谢!

1 个答案:

答案 0 :(得分:0)

进行以下更改:

    DELIMITER $$
DROP FUNCTION IF EXISTS DoUsernameAndPasswordMatch$$
CREATE FUNCTION DoUsernameAndPasswordMatch(
    UserLogin VARCHAR(20),
    UserPass VARCHAR(20)
) RETURNS TEXT
BEGIN
DECLARE userCount TEXT;

    SELECT COUNT(*) INTO userCount
    FROM Users_Login 
    WHERE Users_Login.UserLogin = UserLogin AND Users_Login.UserPass = SHA1(UserPass);

    RETURN userCount;    
END$$
DELIMITER ;