sqlsrv_num_rows

(No version information available, might only be in Git)

sqlsrv_num_rowsRecupera el número de filas de un conjunto de resultados

Descripción

sqlsrv_num_rows(resource $stmt): mixed

Recupera el número de filas de un conjunto de resultados. Esta función requiere que el recurso de consulta haya sido creado con un cursor estático o keyset. Para más información, consulte las funciones sqlsrv_query(), sqlsrv_prepare(), o el capítulo » Especificar un tipo de cursor y seleccionar filas en la documentación de Microsoft SQLSRV.

Parámetros

stmt

La consulta desde la cual se devuelve el número total de filas. El recurso de consulta debe haber sido creado con un cursor estático o keyset. Para más información, consulte las funciones sqlsrv_query(), sqlsrv_prepare(), o el capítulo » Especificar un tipo de cursor y seleccionar filas en la documentación de Microsoft SQLSRV.

Valores devueltos

Devuelve el número total de filas recuperadas en caso de éxito, y false si ocurre un error. Si se utiliza un cursor anterior (por omisión), o un cursor dinámico, se devolverá false.

Ejemplos

Ejemplo #1 Ejemplo con sqlsrv_num_rows()

<?php
$server
= "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" );
$conn = sqlsrv_connect( $server, $connectionInfo );

$sql = "SELECT * FROM Table_1";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $sql , $params, $options );

$row_count = sqlsrv_num_rows( $stmt );

if (
$row_count === false)
echo
"Error al recuperar el número de filas.";
else
echo
$row_count;
?>

Ver también

add a note

User Contributed Notes 2 notes

up
9
wazz3r at gmail dot com
12 years ago
Try to avoid using this function if you need good performance. Specifying "Scrollable" in the options will make you queries take ages to run. If your result contains less than 5000 rows (might vary on different hardware) its faster to not use "Scrollable" and loop over them in php instead.

If you need to check if a result contains rows use "sqlsrv_has_rows()", this function works without "Scrollable". After removing all my "Scrollable" queries, my page loadtime went from 900ms to 60ms.

To demonstrate, here is a query that returns 100 rows:
<?php
for($i = 0; $i < 100; $i++) {
$q = "SELECT sku,name FROM product WHERE visible = 1";
$result = sqlsrv_query($db,$q,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

while(
$row = sqlsrv_fetch_array($result)) {}
}
?>
This takes about 10s! Thats 10 qps..

Now if we remove "Scrollable":
<?php
for($i = 0; $i < 100; $i++) {
$q = "SELECT sku,name FROM product WHERE visible = 1";
$result = sqlsrv_query($db,$q);

while(
$row = sqlsrv_fetch_array($result)) {}
}
?>
This will run in 300ms, about 334 qps!
up
5
smhahmadi
12 years ago
Note that when migrating your MS SQL Server PHP Driver from MSSQL to SQLSRV, if you have used mssql_num_rows, replacing them with sqlsrv_num_rows and replacing mssql_query($query, $mssql_link) with sqlsrv_query($sqlsrv_link, $query) calls will make your sqlsrv_num_rows calls fail. In order to avoid that, you should specify either static, keyset or buffered cursors (buffered cursor has been available since SQLSRV 3.0) when calling sqlsrv_query. For example,
<?php
mssql_query
($query, $mssql_link);
// is equivalent to
sqlsrv_query($sqlsrv_link, $query, array(), array('Scrollable' => 'buffered'));
?>
Using the buffered cursor is "more equivalent" than using the static or keyset cursors to simple mssql_query calls, since it caches the entire result set in client memory.
To Top