Jump to content

Problems with SQLSRV


fRAiLtY-

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]

Link to comment
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.

Link to comment
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' ));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.