PHP bindParam 似乎不适用于 PARAM_INT 输出参数 [英] PHP bindParam does not seem to work with a PARAM_INT out parameter
问题描述
以下是我的 MySQL 存储过程:
The following is my MySQL stored procedure:
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 调用它的方式:
This is how I am calling it from 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 始终保留它的旧值并且不从存储过程接收该值.我在这里做错了什么?
$nmatch always retains it's old value and does not receive the value from the stored procedure. What could I be doing wrong here?
MySQL 服务器版本:5.5.22PHP 版本:5.3.10
MySQL Server Version: 5.5.22 PHP Version: 5.3.10
推荐答案
如 这个博客:
不幸的是,PDO 使用的 MySQL C API 存在错误,这意味着在调用过程时尝试获取输出参数会导致错误:
Unfortunately there’s a bug with the MySQL C API that PDO uses which means that trying to fetch an output parameter when calling a procedure results in the error:
语法错误或访问冲突:例程 $procedure_name 的 1414 OUT 或 INOUT 参数 $parameter_number 不是变量或新的伪变量".
"Syntax error or access violation: 1414 OUT or INOUT argument $parameter_number for routine $procedure_name is not a variable or NEW pseudo-variable".
您可以在 bugs.mysql.com 上查看错误报告.它已在 5.5.3+ 版本中得到修复 &6.0.8+.
You can see the bug report on bugs.mysql.com. It’s been fixed for version 5.5.3+ & 6.0.8+.
要解决此问题,您需要保持 &out 参数分开并调用程序.PHP PDO 文档中的示例 #11 然后将阅读:
To workaround the issue, you would need to keep in & out parameters separate and call the procedure. Example #11 on the PHP PDO documentation would then read:
$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";
这篇关于PHP bindParam 似乎不适用于 PARAM_INT 输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!