简体   繁体   中英

MYSQL Stored procedure giving null value

I have an SP with 2 IN and 4 OUT parameters. It's working fine in MYSQL but not when I call using PHP.

Here is my code:

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());
        }

Here, the var_dump($a) gives ' null ' value.

Any advice as to what's wrong with my code?

Focusing on these two lines:

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

Plan A: Change to

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

Plan B: Change to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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