php - Getting number of rows form SQL Server table -
i have problem getting right value after counted rows table. searched on web didn't find answer.
in database have table categories in have id, , count using column.
i have php code, works there other , better on this?
$sql2 = "select count(id) categories"; $stmt2 = sqlsrv_query($conn, $sql2); $res = sqlsrv_fetch_array($stmt2, sqlsrv_fetch_assoc); foreach($res $row) { $rows = $row; } //if there categories display them otherwise don't if ($rows > 0) { $sql = "select * categories"; $stmt = sqlsrv_query($conn, $sql); while ($row = sqlsrv_fetch_array($stmt, sqlsrv_fetch_assoc)) { echo "<a href='#' class='cat_links'>" . $row['category_name'] . " - <font size='-1'>" . $row['category_description'] . "</font></a>"; } } else { echo "<p style='text-align: center'>no categories yet.</p>"; }
i think has better way convert $stmt2 variable sql resource actual number, or convert $res variable array number. if try echo whole array using foreach, print out number of rows. why use count rows now.
i can't use sqlsrv_num_rows function because error, or no answer.
Comments
Post a Comment