D P.
D P.

Reputation: 1059

Getting number of rows in a sql server table using php

I am trying get the number of rows (or number of primary keys) in a table using PHP and then outputting the result in to a webpage. I am new to SQL server and this wouldn't give me any results

//code above omitted and connected to the database
$query_count = "SELECT COUNT(*) FROM ElectronicShop";
$data_count = sqlsrv_query($connectString, $query_count) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));
//$row_count = sqlsrv_fetch_row($data_count);
echo "<br>ROW COUNT:".sqlsrv_fetch_object($data_count)."<br>";

Upvotes: 0

Views: 551

Answers (1)

ImmortalPC
ImmortalPC

Reputation: 1690

Maybe

//code above omitted and connected to the database
$query_count = "SELECT COUNT(*) AS myCounter FROM ElectronicShop";
$data_count = sqlsrv_query($connectString, $query_count) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));
//$row_count = sqlsrv_fetch_row($data_count);
$row = sqlsrv_fetch_object($data_count);
echo "<br>ROW COUNT:".$row->myCounter."<br>";

From the doc: https://php.net/sqlsrv-fetch-object

Upvotes: 2

Related Questions