简体   繁体   中英

Counting number of rows in Query with PHP

I am trying to count the number of rows in a table, but i am getting the following error:

sqlsrv_num_rows() expects parameter 1 to be resource, array given

Here is my SQL:

<?php   
    $getPriceRequests = "SELECT * FROM PriceRequests";
    $resultPricerequests = sqlsrv_query($conn, $getPriceRequests);
    $priceRequests = sqlsrv_fetch_array($resultPricerequests, SQLSRV_FETCH_ASSOC);  

    $numPriceRequests = sqlsrv_num_rows($priceRequests);
?>

But for some reason when i print out the following i get the above error:

<?php echo $numPriceRequests; ?>

If you want to count the number of rows in the table, then do that in the database, not the application.

The correct query is:

SELECT COUNT(*) as num_rows FROM PriceRequests

Then read the value back into the application.

Changed code to the following for the desired result:

<?php   
    $getPriceRequests = "SELECT * FROM PriceRequests";
    $params = array();
    $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
    $resultPriceRequests = sqlsrv_query($sapconn2, $getPriceRequests, $params, $options);
    $numPriceRequests = sqlsrv_num_rows($resultPriceRequests);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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