duankuaiwang2706 2014-10-17 16:04
浏览 28
已采纳

为什么我需要PDOStatement :: nextRowSet和存储过程来获得结果?

I don't understand PDO's behavior about stored procedure and I need some explanations (and maybe a solution to my problem).

I'm using SQL Server 2008 R2 with PHP 5.4 and Apache 2.2. I'm calling a stored procedure with 2 parameters using PDO (with SQLSRV drivers):

$pdoStmt = $pdo->prepare('MY_STORED_PROCEDURE ?, ?');
$pdoStmt->bindValue(1, 'MyValue1', PDO::PARAM_STR);
$pdoStmt->bindValue(2, 'MyValue2', PDO::PARAM_STR);
$pdoStmt->execute();

Okay, here, everything works perfectly. The result of the stored procedure called is an array so I should use fetchArray(). So I do that:

$result = $pdoStmt->fetchArray();

BUT the result is empty. And I don't know why, I have to call several time nextRowSet() to get the result. So I do that:

do {
    $result = $pdoStmt->fetchAll(PDO::FETCH_ASSOC);
} while ($pdoStmt->nextRowSet());

Then I have my result ! Yay....

The stored procedure works when I execute it directly in the SQL Server (I have a correct result with the same parameters).

So why I have to do that and is there any solution about that ? I don't want to call nextRowSet() for nothing...

Thanks in advance for your response.

  • 写回答

2条回答 默认 最新

  • douyouqian8550 2014-11-14 10:42
    关注

    After researches, here my own fetchArray() method :

    function fetchArray($single = false)
    {
        $this->row = array();
        do {
            $tmp = $this->cur->fetchAll(PDO::FETCH_ASSOC);
            $cnt = $this->cur->columnCount();
            if($cnt) {
                $this->nb_fields = $cnt;
                for($i=0; $i<$cnt; $i++){
                    $this->fields[] = $this->cur->getColumnMeta($i)['name'];
                }
            }
            $this->row = array_merge($this->row, $tmp);
        } while ($this->cur->nextRowSet());
        if (count($this->row) == 1 && $single == true) {
            $this->row = $this->row[0];
        }
        if (empty($this->row)) {
            $this->row = false;
        }
        return $this->row;
    }
    

    Hope that will help :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像