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 />";
}else{
     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>";

$query=sqlsrv_query($conn,$sql);
             
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;
LVL 1
Matthew RoessnerSenior Systems ProgrammerAsked:
Who is Participating?
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:

http://php.net/manual/en/function.sqlsrv-num-rows.php
0
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.
0
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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.
0

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
0
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!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.