我需要从MySQL存储过程中检索OUT参数。我找不到任何能解释这一点(对我来说很有意义)的东西。

try {
$dsn = 'mysql:dbname=db_name;host=localhost';
$dbh = new PDO($dsn, 'usr_name', 'password');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

$stmt = $dbh->prepare("CALL db.stprNewUser(:usrEmail,:newUserOK,:stprComment)");

$stmt->bindParam(':usrEmail', $tmpEmail, PDO::PARAM_STR);
$stmt->bindParam(':newUserOK', $newUserOK, PDO::PARAM_INT,1);
$stmt->bindParam(':stprComment', $stprComment, PDO::PARAM_STR,100);

$stmt->execute();

$outputArray = $dbh->query("select @newUserOK, @stprComment")->fetch(PDO::FETCH_ASSOC);

print "procedure returned [" . $outputArray['@newUserOK'] . $outputArray['@stprComment'] . "]\n";


我在另一个SO项目上找到了最后两行,但它只返回NULL值。

最佳答案

尝试这个...
看看是否可行...

try
{
    $dsn = 'mysql:dbname=db_name;host=localhost';
    $dbh = new PDO($dsn, 'usr_name', 'password');
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}


//$stmt = $dbh->prepare("CALL db.stprNewUser(:usrEmail,:newUserOK,:stprComment)");
//changed :newUserOK to @newUserOK
//changed :stprComment to @stprComment
$stmt = $dbh->prepare("CALL db.stprNewUser(:usrEmail,@newUserOK,@stprComment);");

//declare only input parameters.
//good pratice put string length. assuming varchar(100).
$stmt->bindParam(':usrEmail', $tmpEmail, PDO::PARAM_STR,100);

//dont need these
// $stmt->bindParam(':newUserOK', $newUserOK, PDO::PARAM_INT,1);
// $stmt->bindParam(':stprComment', $stprComment, PDO::PARAM_STR,100);

$stmt->execute();

$outputArray = $dbh->query("select @newUserOK, @stprComment;")->fetchAll();

foreach($outputArray as $row)
{
   "NewUserOk:" .  $row["@newUserOk"] . ", StprComment:" . $row["@stprComment"];
}

//$outputArray = $dbh->query("select @newUserOK, @stprComment")->fetch(PDO::FETCH_ASSOC);
//print "procedure returned [" . $outputArray['@newUserOK'] . $outputArray['@stprComment'] . "]\n";

关于php - PHP PDO/从MySQL存储过程检索OUT参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15740724/

10-10 03:31