简体   繁体   English

PDO行数-等效num_rows

[英]PDO row count - num_rows equivalent

I'm moving PHP code from mysql to ms sql server. 我将PHP代码从mysql移到ms sql服务器。 This will execute my query: 这将执行我的查询:

  $r = $db_conn->prepare($sql);
  $r->execute();

Before I start processing, I need to count how many rows were returned. 在开始处理之前,我需要计算返回的行数。

old code: 旧代码:

$r->num_rows;

new code: 新代码:

$rows  = $r->fetchAll(PDO::FETCH_ASSOC);
$rcount = count($rows);

All good, but when I try to access values from 1st row I'm getting nothing... 一切都很好,但是当我尝试从第一行访问值时,我什么也没得到...

try {
    $row = $r->fetch(PDO::FETCH_ASSOC);
} catch (Exception $ex) {   
    return 0;
}   

I need to repoint to 1st row in my recordset after doing the count, how can I do this without having to requery the database again? 计数后,我需要指向记录集的第一行,如何做到这一点而不必再次重新查询数据库?

I'm new to PHP, sorry for dumb and probably obvious question. 我是PHP的新手,对您的愚蠢和明显的问题感到抱歉。 Thanks in advance. 提前致谢。

There isn't a way in PDO to reset the pointer. PDO中没有办法重置指针。 You could execute the query again, but given that you've already fetched all the data with your call to fetchAll , that is wasteful. 您可以再次执行查询,但是鉴于您已经通过调用fetchAll来获取了所有数据,这很浪费。 Instead, to access the data, simply loop over the $rows array eg 相反,要访问数据,只需在$rows数组上循环即可,例如

foreach ($rows as $row) {
    // do something
}

Note that if you expect an exception on fetch , it is likely you might get one on fetchAll too, so you should wrap the call to fetchAll in a try/catch block: 请注意,如果您期望fetch发生异常,则很有可能也会在fetchAll上获得一个异常,因此您应该将对fetchAll的调用包装在try / catch块中:

try {
    $rows = $r->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $ex) {   
    return 0;
}   

One possible approach, if you use PHP Driver for SQL Server , is to use PDOStatement::rowCount and client-side cursor . 如果使用PHP Driver for SQL Server ,一种可能的方法是使用PDOStatement :: rowCount客户端游标 With this type of cursor row count is available after a query is executed, but this type of cursor should be used for small (medium) result sets. 使用这种类型的游标,行数在执行查询后可用,但是这种类型的游标应用于小型(中型)结果集。

<?php
# Connection
$server    = 'server\instance,port';
$database  = 'master';
$uid       = 'uid';
$pwd       = 'pwd';

# PDO Connection
try {
    # SQL authentication
    $conn = new PDO("sqlsrv:server=$server;Database=$database", $uid, $pwd);
    # Windows authentication
    #$conn = new PDO("sqlsrv:server=$server;Database=$database");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch( PDOException $e ) {
    die( "Error connecting to SQL Server".$e->getMessage());
}

# Client-side cursor
try {
    $options = array(
        PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, 
        PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED
    );
    $stmt = $conn->prepare("SELECT * FROM master.sys.server_principals", $options);
    $stmt->execute();
    echo 'Row count: '.$stmt->rowCount().'<br>';
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC) ){
        echo 'Login name: '.$row['name'].'<br>';
    }    
} catch( PDOException $e ) {
    die( "Error executing query".$e->getMessage() );
}

# End
$stmt = null;
$conn = null;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM