fRAiLtY- Posted June 28, 2011 Share Posted June 28, 2011 Hi, I'm using SQLSRV from Microsoft. I can connect and list results from the database just fine, however I'm getting a very odd result. I have a simple if statement I've used loads of times before in the form of this: // Check for 0 results if(sqlsrv_num_rows($lapsed) == 0) { echo "No results"; } However, the result set returns (as it should as theres ~40 records) yet it still says "No results" at the top? It echo's that statement irrespective of the result. Ironically the complex query works fine and as it should however the simple if statement doesn't. sqlsrv_num_rows returns "false" constantly. I did a var_dump(sqlsrv_num_rows($lapsed)); to ascertain this. On the plus side the results output great! Just no error checking there Here's my complete code, using PHP 5.3.5 on a Windows box connecting to MSSQL Server 2008 R2. <?php // Connect to database require('inc/db.php'); // Query $sql = "SELECT * FROM JobOperation INNER JOIN MainJobDetails ON JobOperation.JobNo = MainJobDetails.JobNo WHERE Started IS NULL AND Code = 'STUDIO' AND Duration > '15' AND JobOperation.LastOpCode IS NULL AND JobCompleted = '0' ORDER BY JobOperation.JobNo DESC"; $lapsed = sqlsrv_query($db,$sql); // Check for 0 results if(sqlsrv_num_rows($lapsed) == 0) { echo "No results"; } // Loop through data while($row = sqlsrv_fetch_array($lapsed)) { echo $row['JobNo'] . " " . $row['InvoiceCustomerName'] . " " . date_format($row['CreateDateTime'], 'Y-m-d H:i:s') . "<br />"; } ?> Cheers. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/240618-problems-with-sqlsrv/ Share on other sites More sharing options...
mikosiko Posted June 28, 2011 Share Posted June 28, 2011 read the description for the first option showed here... specifically the note regarding what sqlsrv_num_rows return. http://msdn.microsoft.com/en-us/library/ee376927.aspx Quote Link to comment https://forums.phpfreaks.com/topic/240618-problems-with-sqlsrv/#findComment-1235885 Share on other sites More sharing options...
fRAiLtY- Posted June 28, 2011 Author Share Posted June 28, 2011 Hi, I should mention also that I've tried assigning a cursor type of both KEYSET and STATIC and no change. I did this by doing this: $lapsed = sqlsrv_query($db,$sql,array( "Scrollable" => 'keyset' )); and $lapsed = sqlsrv_query($db,$sql,array( "Scrollable" => 'static' )); sqlsrv_num_rows returns "false" irrespective of the cursor type. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/240618-problems-with-sqlsrv/#findComment-1235892 Share on other sites More sharing options...
fRAiLtY- Posted June 28, 2011 Author Share Posted June 28, 2011 I should add also that upon applying the cursor types (STATIC and KEYSET) as above I get the following error both times: sqlsrv_num_rows() expects parameter 1 to be resource, boolean given Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/240618-problems-with-sqlsrv/#findComment-1235903 Share on other sites More sharing options...
mikosiko Posted June 28, 2011 Share Posted June 28, 2011 and did you notice that sqlsrv_query() syntax has 4 parameters, been the last 2 inclusively optionals? sqlsrv_query( resource $conn, string $tsql [, array $params [, array $options]]) in other words... your statement: $lapsed = sqlsrv_query($db,$sql,array( "Scrollable" => 'keyset' )); seems that need to be written in this way: $lapsed = sqlsrv_query($db,$sql,array(), array( "Scrollable" => 'keyset' )); Quote Link to comment https://forums.phpfreaks.com/topic/240618-problems-with-sqlsrv/#findComment-1235925 Share on other sites More sharing options...
fRAiLtY- Posted June 28, 2011 Author Share Posted June 28, 2011 That's excellent, I missed that they were inclusively optional. Thanks for your help. I'll mark this as solved. Quote Link to comment https://forums.phpfreaks.com/topic/240618-problems-with-sqlsrv/#findComment-1235926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.