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条)

报告相同问题?

悬赏问题

  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多