PHP connecting to SQL Issues

I want to use PHP to connect to a SQL 2008 database that I have.  I have attempted to connect to the database and I am able to establish a connection. But when I attempt to return the number of rows of a table - I get the wrong number of rows.

If I run the query within SQL Management Studio, I return a result of 25922.  If I run the query using PHP, I return a result of 16,914

I am not sure why it might be doing this...

Below is a snippet of the code I am using to connect

$serverName = 'servername\NewTech';

$connectionInfo = array( "Database"=>"DatabaseName", "UID"=>"Username", "PWD"=>"Password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));

$sql='select COUNT(*) from DatabaseName.Person.Patient a (nolock)';

echo "SQL: ".$sql."<br>";

if( sqlsrv_fetch( $query ) === false) {
     die( print_r( sqlsrv_errors(), true));

// Get the row fields. Field indeces start at 0 and must be retrieved in order.
// Retrieving row fields by name is not supported by sqlsrv_get_field.

$name = sqlsrv_get_field( $query, 0);

echo "Number of Rows: ".$name;
Matthew RoessnerSenior Systems ProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I have been using 'sqlsrv_num_rows()' to get the row count in PHP.  I don't know what the difference is in the methods but you might want to try the example on this page:
Matthew RoessnerSenior Systems ProgrammerAuthor Commented:
I have tried using SQLSRV_NUM_ROWS and it returns the same results.  For some reason - when I go through PHP, I do not get the full result set that I would expect. I don't know if there is a limitation or restriction set in PHP somewhere or what might be the problem.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Are you sure that you are connecting to the same server?
SELECT @@SERVERNAME returns the SQL Instance that you are connected to. It might be good if you confirm it.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Dave BaldwinFixer of ProblemsCommented:
I asked if you tried the complete example on that page.  It says there are restrictions on how and when you can use some of the methods.  Don't merely substitute SQLSRV_NUM_ROWS into your code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matthew RoessnerSenior Systems ProgrammerAuthor Commented:
It turns out that the issue was related to some of the parameters in the QUERY command. The SQLSRV_NUM_ROWS wasn't the issue, but the URL you reference led me to looking into some of the parameters and ultimately I was able to resolve the issue.

Thanks for your help
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.  I suggested SQLSRV_NUM_ROWS so you could get a different view on the problem.  Seems to have at least indirectly succeeded!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.