get the number of rows of a sql server base to show in php

1

I want to see the number of rows in a table in SQL server my sentence is:

$result12 = sqlsrv_num_rows($conn, "SELECT asistencia FROM asistencia");

but you are not giving me any results when trying to show it in php, taking 100 results in my database

<?php echo $result12; ?>
    
asked by claus 30.07.2018 в 18:06
source

2 answers

2

Try it like this:

$result2 = sqlsrv_query( $conn, "SELECT asistencia FROM asistencia");

$result = sqlsrv_num_rows( $result2 );

echo $result;
    
answered by 30.07.2018 / 18:13
source
0

I finally found the problem
referring to the user alalp:

  

It is because sqlsrv_query () uses SQLSRV_CURSOR_FORWARD cursor type by   default However, in order to get a result from sqlsrv_num_rows (), you   should choose one of these cursor types below:

     

SQLSRV_CURSOR_STATIC SQLSRV_CURSOR_KEYSET   SQLSRV_CURSOR_CLIENT_BUFFERED For more information, check: Cursor   Types (SQLSRV Driver)

     

In conclusion, if you use your query like:

     

$ query = sqlsrv_query ($ conn, $ result, array (), array ("Scrollable" = >   'static')); you will get result in:

     

$ row_count = sqlsrv_num_rows ($ query);

The problem is that in SQL serverno elcount (*) or sqlsrv_num_rows since it is a different method as it recovers the information, so nothing but delivering a result

the correct method is (in sqlsrv)

<?php
$sql = "SELECT * FROM asistencia";
$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 in retrieveing row count.";
else
   echo $row_count;
?>

If I find it hard to find why you do not deliver results in the way you shared (and thank you very much again) the companion alanfcm

I hope it's useful to someone.

    
answered by 30.07.2018 в 19:14