PHP bindParam似乎不适用于PARAM_INT out参数

时间:2012-05-03 09:20:31

标签: php mysql stored-procedures output-parameter

以下是我的MySQL存储过程:

DROP PROCEDURE IF EXISTS sp_authenticate;
CREATE PROCEDURE sp_authenticate(IN user_name VARCHAR(50),
  IN password1 VARCHAR(50), OUT nmatch INT)
BEGIN

  SELECT COUNT(*) INTO nmatch
    FROM user1 u
    WHERE u.name = user_name AND
      u.password1 = password1;

END//

这就是我从PHP调用它的方式:

function authenticate($pdo, $user_name, $password){
  $stmt = $pdo->prepare('CALL sp_authenticate(:user_name, :password, :nmatch)');
  $stmt->bindValue(':user_name', $user_name, PDO::PARAM_STR);
  $stmt->bindValue(':password', $password, PDO::PARAM_STR);
  $nmatch = 888888;
  $stmt->bindParam(':nmatch', $nmatch, PDO::PARAM_INT, 4);
  $result = $stmt->execute();

  return $nmatch;
}

$ nmatch始终保留旧值,并且不从存储过程接收值。我在这里做错了什么?

MySQL服务器版本:5.5.22 PHP版本:5.3.10

1 个答案:

答案 0 :(得分:1)

this blog中所述:

  

不幸的是,PDO使用的MySQL C API存在一个错误,这意味着在调用过程时尝试获取输出参数会导致错误:

     
    

“语法错误或访问冲突:1414 OUT或INOUT参数$ parameter_number用于例程$ procedure_name不是变量或NEW伪变量”。

  
     

您可以在bugs.mysql.com上看到错误报告。它已修复5.5.3+版本和6.0.8 +。

     

要解决此问题,您需要保持& out参数分开并调用该过程。然后Example #11 on the PHP PDO documentation会读到:

$stmt = $dbh->prepare("CALL sp_takes_string_returns_string(:in_string, @out_string)");
$stmt->bindParam(':in_string', 'hello');

// call the stored procedure
$stmt->execute();

// fetch the output
$outputArray = $this->dbh->query("select @out_string")->fetch(PDO::FETCH_ASSOC);

print "procedure returned " . $outputArray['@out_string'] . "\n";
相关问题