Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我需要PDOStatement::nextRowSet和存储过程来获得结果?_Php_Sql Server_Sql Server 2008_Stored Procedures_Pdo - Fatal编程技术网

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

Php 为什么我需要PDOStatement::nextRowSet和存储过程来获得结果?,php,sql-server,sql-server-2008,stored-procedures,pdo,Php,Sql Server,Sql Server 2008,Stored Procedures,Pdo,我不理解PDO关于存储过程的行为,我需要一些解释(也许还有我的问题的解决方案) 我将SQLServer2008R2与PHP5.4和Apache2.2一起使用。 我正在使用PDO(使用SQLSRV驱动程序)调用一个具有2个参数的存储过程: 好的,这里,一切都很好。调用的存储过程的结果是一个数组,因此我应该使用fetchArray()。所以我这样做: $result = $pdoStmt->fetchArray(); do { $result = $pdoStmt->fetch

我不理解PDO关于存储过程的行为,我需要一些解释(也许还有我的问题的解决方案)

我将SQLServer2008R2与PHP5.4和Apache2.2一起使用。 我正在使用PDO(使用SQLSRV驱动程序)调用一个具有2个参数的存储过程:

好的,这里,一切都很好。调用的存储过程的结果是一个数组,因此我应该使用fetchArray()。所以我这样做:

$result = $pdoStmt->fetchArray();
do {
    $result = $pdoStmt->fetchAll(PDO::FETCH_ASSOC);
} while ($pdoStmt->nextRowSet());
但结果是空的。我不知道为什么,我必须多次调用nextRowSet()才能得到结果。所以我这样做:

$result = $pdoStmt->fetchArray();
do {
    $result = $pdoStmt->fetchAll(PDO::FETCH_ASSOC);
} while ($pdoStmt->nextRowSet());
那我就有结果了!耶

当我直接在SQL Server中执行存储过程时,它就会工作(使用相同的参数得到正确的结果)

那我为什么要这么做,有什么解决办法吗?我不想无缘无故地调用nextRowSet()


提前感谢您的回复。

PDO中没有
fetchArray()
方法,如果您想获取数组,可以执行以下操作:

$result = $pdoStmt->fetch(PDO::FETCH_ASSOC);

获取关联数组。

经过研究,这里是我自己的
fetchArray()
方法:

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;
}
函数fetchArray($single=false) { $this->row=array(); 做{ $tmp=$this->cur->fetchAll(PDO::FETCH_ASSOC); $cnt=$this->cur->columnCount(); 若有($cnt){ $this->nb_字段=$cnt; 对于($i=0;$ifields[]=$this->cur->getColumnMeta($i)['name']; } } $this->row=array\u merge($this->row,$tmp); }而($this->cur->nextRowSet()); 如果(计数($this->row)==1&&$single==true){ $this->row=$this->row[0]; } if(空($this->row)){ $this->row=false; } 返回$this->row; }
希望这会有所帮助:-)

谢谢您的回复!我试过了,但还是有同样的行为。@VincentNicopolsky您的过程包含多少sql语句?如果有多个参数,则只需要
nextRowSet()
。有一个条件可以检查参数是否为空。如果它是空的,我将执行一个
选择'FALSE'作为结果
否则我将执行一个
从MyTable中选择R1,R2,当R1=@MyValue1和R2=@MyValue2
时。只有这一点:-/