简体   繁体   中英

PHP MS SQL Count number of rows is empty

I'm trying to put together a table that displays events by date range. Which works with the code below. My next step is pagination, learning as I go. From everything I've read, I must get the number of rows to determine if there is enough to begin a 'next page' to move on to the next step.

I'm stumped on this step. It seems my

$row_count = sqlsrv_num_rows( $stmt);
echo 'numRows: ',$row_count;

is not returning anything.

numRows: 'this is empty'

<head>
<title>Paginate</title>
</head>
<body>
<form method='get'>
<?PHP
$sql = "Select I.IncidentNumber, o.CaseCode , O.CaseDescription, V.HomeAddress
From tblIncident I
left join
tblIncidentType O
on O.IncidentNumber=I.IncidentNumber
left join
tblIncidentCustomer V
on V.IncidentNumber=I.IncidentNumber
Where IncidentDate between '01/01/2017' and '01/02/2017'
order by I.IncidentNumber";

 $stmt = sqlsrv_query( $conn, $sql);

if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true)) ;
}

/**
 * Put resules in a table
 */
$stmt = sqlsrv_query($conn,$sql);
$row_count = sqlsrv_num_rows( $stmt);
echo 'numRows: ',$row_count;

    echo "<table border='5' column width='700'><tr><th><center>Incident Number</center></th><th><center>CaseCode</center></th><th><center>Case Description</center></th><th><center>Home Address</center></th></><th></></tr>";


while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) 

{
    echo "<tr>";
    echo "<td><center>" . $row['IncidentNumber']. "</center></td>";
    echo "<td><center>" . $row['CaseCode']."</center></td>";
    echo "<td><center>" . $row['CaseDescription']."</center></td>";
    echo "<td><center>" . $row['HomeAddress']."</center></td>";
    }
    echo "</table>"; 

    ?>
</body>
</html>

I apologize in advance but I am trying to learn as I go but this has me stumped. I'm sure there is a duplicate with an answer but everything I have tried from the other QAs are not working. Any help you can give would be appreciated.

我认为它应该是“O.CaseCode”而不是“o.CaseCode”?

From the documentation : Function sqlsrv_num_rows() requires a client-side, static, or keyset cursor, and will return false if you use a forward cursor or a dynamic cursor (a forward cursor is the default). You may try with this:

...
$stmt = sqlsrv_query($conn, $sql, array(), array("Scrollable" => 'static'));
if( $stmt === false ) {
    echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
    exit;
}
$row_count = sqlsrv_num_rows($stmt);  
if ($row_count === false) { 
    echo "Error (sqlsrv_num_rows)";
    exit;
}
...

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