简体   繁体   English

MySQL存储过程PDO :: fetch()在返回null时引发一般错误

[英]MySQL Stored procedure, PDO::fetch() throws general error on null return

The problem is that I'm using a stored procedure to handle validation of my accounts. 问题是我正在使用存储过程来处理我的帐户的验证。 (Please note that this is a dumbed down version of the procedure and method and is only used for demonstration purposes). (请注意,这是该过程和方法的简化版本,仅用于演示目的)。 Note : Account authentication is not the original purpose of this method, once again just for example. 注意 :帐户身份验证不是此方法的初衷,仅举例来说。

Here's my Example MySQL procedure: 这是我的示例MySQL过程:

BEGIN
    DECLARE rc INT(11);
    DECLARE new VARCHAR(40);
    SET new = uuid();

    UPDATE accounts SET session_key = new WHERE account_id = id_in AND session_key = session_in;

    SELECT ROW_COUNT() into rc;

    IF(rc > 0) THEN
        SELECT new AS `session_key`;
    END IF;

END

Here's the PHP related code that goes with it: 这是附带的与PHP相关的代码:

    private static function authenticate() {
        $connection = Database::getConnection();
        $account = 19;
        $session = "cc73e13b-2983-11e5-8ade-00163e603fb4";
        $statement = $connection->prepare("CALL AUTH_SESSION(:account, :session);");
        $statement->bindParam(":account", $account);
        $statement->bindParam(":session", $session);
        if($statement->execute()) {
            if($row = $statement->fetch()) {
                echo 'valid session';
            } else {
                echo 'invalid session';
            }
        } else {
            echo 'failed to execute query';
        }
    }

When the session_key and account_id are correct, the procedure prints out (or SELECTS) the new session_key so that PHP can obtain it. 当session_key和account_id正确时,该过程将打印(或选择)新的session_key以便PHP可以获取它。 However when one of the two values are not correct the SELECT statement is never called. 但是,当两个值之一不正确时,将永远不会调用SELECT语句。

I figured this would be fine. 我认为这会很好。 If a row is returned->get the session key (Success) and if the row isn't returned, the validation failed, but it apparently doesn't work that way. 如果返回一行->获取会话密钥(成功),并且如果未返回该行,则验证失败,但是显然不能那样进行。

The error returned is as follows: 返回的错误如下:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error'

Just to re-iterate, this error is only thrown when (!(rc>0)) . 仅重申一下,此错误仅在(!(rc>0))时抛出。

You can only use fetch() if the query returns a result set, ie the stored procedure has to return the result of a SELECT . 仅当查询返回结果集时才可以使用fetch() ,即存储过程必须返回SELECT的结果。

You could have the procedure return an empty result set when rc > 0 is false. rc > 0为false时,您可以让过程返回空结果集。

BEGIN
    DECLARE rc INT(11);
    DECLARE new VARCHAR(40);
    SET new = uuid();

    UPDATE accounts SET session_key = new WHERE account_id = id_in AND session_key = session_in;

    SELECT ROW_COUNT() into rc;

    SELECT new AS session_key
    FROM dual
    WHERE rc > 0;

END

When you call fetch() it will return false if the result set is empty, otherwise it will return an array containing the session key. 调用fetch() ,如果结果集为空,它将返回false ,否则将返回包含会话密钥的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM