PDO输出参数错误

时间:2012-02-11 14:45:52

标签: pdo output-parameter

我正在使用这个SP,在使用mysql workbench时得到了两个结果。

CREATE PROCEDURE SP(IN _start INT,IN _end INT,INOUT _count INT)
BEGIN

   SET _count = (SELECT COUNT(*) FROM tbl);

   SET @qry = CONCAT('select * from tbl limit ', _start, ',', _end);

   PREPARE stmt FROM @qry;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt;

END

但与PDO一起使用时会返回此错误

$c=0;
$stmt = $this->_dbc->getConnection()->prepare("CALL SP(0,10,:count)");
    $stmt->bindParam(":count",$c,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT,0);
    $stmt->execute();
    return $c;
PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1414 OUT or INOUT argument 3 for routine db22.SP is not a variable or NEW pseudo-variable in BEFORE trigger

但是在改变

$this->_dbc->getConnection()->prepare("CALL SP(0,10,**:count**)");

$this->_dbc->getConnection()->prepare("CALL SP(0,10,@count)");

我没有返回任何错误,但始终将计数设为0。

  1. 区别:count和@count?
  2. 如何通过pdo获得准确的计数?

1 个答案:

答案 0 :(得分:4)

您可以执行以下解决方法:

$dbh = $this->_dbc->getConnection()
$stmt = $dbh->prepare("CALL SP(0,10,@count)");
$stmt->execute();

$sql = "SELECT @count AS count";
$stmt = $dbh->prepare($sql);
$stmt->execute();

您可以在此处找到更多信息:http://www.php.net/manual/en/pdo.prepared-statements.php#101993