Best way to check for empty set from a query using MS SQl and PHP 5.5

What is the best way to  check for a query returning an empty set, (no rows found), using MS SQL Server and PHP 5.5

I am using php_sqlsrv_55_nts.dll driver.

This is what I use but there must be a shorter way.....appreciate knowing if one exists

$sql = "SELECT * FROM MyTable WHERE MyId = $nuid AND mycode = '$nucode'";
                        echo $sql;
                        $params = array();
                        $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
                        $stmt = sqlsrv_query( $conn, $sql , $params, $options );
                        $row_count = sqlsrv_num_rows( $stmt );
                        echo $row_count;
                        if ($row_count === FALSE)
                           {
                                echo "No Match";
                                 session_destroy();
                                header ("Location: NotFound.php");
                             }

Open in new window

OverthereAsked:
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:
If the query is valid but doesn't return any rows, the 'row_count' will be 0 which is not exactly '===' the same as false.
0
OverthereAuthor Commented:
So are you saying it would be better, and I would think so, to test it this way..
if ($row_count == 0)
   blah blah...

Open in new window


It seems like a lot of coding just to check for no rows....I thought there might be a shorter way to do so....
0
Peos JohnPHPCommented:
You can use empty function to check this.

if( empty( $row_count) )
{
     // Your operations....
}
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Dave BaldwinFixer of ProblemsCommented:
'empty' won't work because $row_count is being assigned a value.

'if ($row_count == 0)' is the most basic way of checking the value of a variable.  I don't see how it could be any simpler.
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
Ray PaseurCommented:
... seems like a lot of coding just to check for no rows ...
Actually it's just one line - an if() statement.  All the "other stuff" is about running the query and testing for success, etc., and it's necessary "overhead" of dealing with databases.  Most of us package this "stuff" in abstraction layers so we never have to look at it!  

This article specifically deals with MySQL, but the principles it illustrates are the same for all databases.
http://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
OverthereAuthor Commented:
In my years of coding, it seems that "empty", "null" and "assign value" are greatly misunderstood and misused.  Understanding the differences are so very important when coding/testing.
That being said, and taking into account David and Rays input, I think I have my answer...
Thank you fellas.
The help was appreciated. I have another question to ask but not sure how to verbalize...sigh
0
Ray PaseurCommented:
empty(), NULL, isset(), etc may use type coercion in PHP, as can comparison operators.
http://php.net/manual/en/types.comparisons.php
http://php.net/manual/en/language.types.type-juggling.php
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
PHP

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.