Jump to content

Archived

This topic is now archived and is closed to further replies.

fRAiLtY-

Problems with SQLSRV

Recommended Posts

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 :wtf:

 

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]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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' ));

Share this post


Link to post
Share on other sites

That's excellent, I missed that they were inclusively optional.

 

Thanks for your help. I'll mark this as solved.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.