0

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>";
1
  • 3
    Perhaps RTFMing would help: php.net/sqlsrv_fetch_object. You're fetching an OBJECT representing a row of results from your query, not the actual value of the count you did. Since you're fetching an object, your echo is trying to output that object, and will fail since whatever class that object is, it won't have a toString()-type auto method. Commented Jul 21, 2014 at 14:34

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.