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";
                                header ("Location: NotFound.php");

Open in new window

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:
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.
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....
Peos JohnPHPCommented:
You can use empty function to check this.

if( empty( $row_count) )
     // Your operations....
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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.

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.
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
Ray PaseurCommented:
empty(), NULL, isset(), etc may use type coercion in PHP, as can comparison operators.
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

From novice to tech pro — start learning today.