MYSQL存储过程给出null值

时间:2015-03-30 17:27:20

标签: php mysql

我有一个带2个IN和4个OUT参数的SP。它在MYSQL中运行良好,但在我使用PHP调用时却没有。

这是我的代码:

try{
            //var_dump($mysqli);
            // bind the first parameter to the session variable @uid
            $stmt = $mysqli->prepare('SET @tid := ?');
            $stmt->bind_param('i', $tid);
            $stmt->execute();

            // bind the second parameter to the session variable @userCount
            $stmt = $mysqli->prepare('SET @mid := ?');
            $stmt->bind_param('i', $memID);
            $stmt->execute();

            // execute the stored procedure
            $sql_1 = 'CALL supplyRSummary(@tid,@mid,@a,@na,@r,@nv)';
            $stmt  = $mysqli->prepare($sql_1);

            // execute the second query to get values from OUT parameter
            $sql_2 = 'SELECT @a,@na,@r,@nv';
            $result = $mysqli->query($sql_2);
            $rows = $result->fetch_assoc();

            if ($rows) {
                $a=$rows['a'];
                $na=$rows['na'];
                $r=$rows['r'];
                $nv=$rows['nv'];

                var_dump($a);
            }
        }catch (PDOException $pe){
            die("Error occurred:" . $pe->getMessage());
        }

此处,var_dump($a)会给出“null”值。

有关我的代码有什么问题的建议吗?

1 个答案:

答案 0 :(得分:0)

关注这两行:

$sql_2 = 'SELECT @a,@na,@r,@nv';
$a=$rows['a'];

计划A:改为

$sql_2 = 'SELECT @a AS a, @na AS na, @r AS r, @nv AS nv';
$a=$rows['a'];

计划B:改为

$sql_2 = 'SELECT @a,@na,@r,@nv';
$a=$rows['@a'];