Solved

PHP SELECT Using SQLSVR driver from SQL Server Function returns unexpected result.

Posted on 2014-04-28
7
3,743 Views
Last Modified: 2014-04-29
I have the following code

	$serverName = "<myserver>";
	$uid = "<myuid>";
	$pwd = "<mypassword>";
	$connectionInfo = array( "UID"=>$uid,"PWD"=>$pwd,"Database"=>"<mydatabase>");
	$conn = sqlsrv_connect($serverName, $connectionInfo);
	
	if( $conn === false )
		{
			$status = "0044";
			header('Location: error.asp?1='.$status);
			exit();
		}

	$SQLStmt = "SELECT [dbo].ufn_PreStripHTML('".$_POST['headline']."') AS Headline;";
	$RS_Headline01 = sqlsrv_query($conn, $SQLStmt); 

	if( $RS_Headline01 === false )
		{
			$status = "0044";
			header('Location: error.asp?1='.$status);
			exit();
		}

// Make row available for reading.

	if (sqlsrv_fetch($RS_Headline01) === false)
		{
			$status = "0044";
			header('Location: error.asp?1='.$status);
			exit();
		}

// Get the row field

	$Headline = sqlsrv_get_field( $RS_Headline01,0);
	echo($Headline);

Open in new window


The tested the $SQLStmt content by running it through SSMS and the query it produced was just fine (it returned a parsed string - as expected),

$Headline should be return the parsed string, but instead just returns 'Resource id #4 '.

Can anyone help please?

Regards,
0
Comment
Question by:splanton
  • 3
  • 2
  • 2
7 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40027935
Hmm... Maybe this is one of the reasons that the SQLSRV extension is not used very much!

According to the man page, there are only two Resource types returned...
http://php.net/manual/en/sqlsrv.resources.php

... and neither of the Resources is returned by sqlsrv_get_field().
http://php.net/manual/en/function.sqlsrv-get-field.php

From reading your code and the man pages, it looks to me like you're doing it right, but getting incorrect output.  Have you tried using var_dump() on each of the return values from the SQLSRV extension calls?  There might be a clue in the output from something like that.
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 450 total points
ID: 40028184
You're trying too hard.  Examples #1 and #2 on this page http://www.php.net/manual/en/function.sqlsrv-fetch-array.php work perfectly (if you substitute valid data).  Much like the MySQL driver, the query returns a Resource and the Resource is used to get the actual data.

Example #1
<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT FirstName, LastName FROM SomeTable";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      echo $row['LastName'].", ".$row['FirstName']."<br />";
}

sqlsrv_free_stmt( $stmt);
?>

Open in new window

Example #2
<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT FirstName, LastName FROM SomeTable";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
      echo $row[0].", ".$row[1]."<br />";
}

sqlsrv_free_stmt( $stmt);
?>

Open in new window

0
 
LVL 2

Author Comment

by:splanton
ID: 40029174
Hi,
Well, I may be trying too hard :)

I tested the code - great - it works on a standard select statement, but returns a:

PHP Notice:  Undefined offset: 1 in C:\inetpub\wwwroot\...........etc

Error when used with a SQL Server Function that works in SSMS and in ASP classic.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 40029226
Whenever you get a PHP Notice, you can use var_dump() to print out the variable in question (it will be on the right side of an assignment statement, usually).  
http://php.net/manual/en/function.var-dump.php

Often this will tell what happened in the script to produce unexpected data structures.
0
 
LVL 2

Author Comment

by:splanton
ID: 40029593
OK, got it working with the following:

<?php
	$serverName = "<MyInstance>";
	$uid = "MyUID";
	$pwd = "MyPWD";
	$connectionInfo = array( "UID"=>$uid,"PWD"=>$pwd,"Database"=>"<MyDatabase>");
	$conn = sqlsrv_connect($serverName, $connectionInfo);
	
	if( $conn === false )
		{
			$status = "0044";
			header('Location: error.asp?1='.$status);
			exit();
		}

	$SQLStmt = "SELECT [dbo].ufn_PreStripHTML('".$_POST['headline']."') AS Headline;";
	$RS_Headline01 = sqlsrv_query($conn, $SQLStmt); 

	if( $RS_Headline01 === false )
		{
			$status = "0044";
			header('Location: error.asp?1='.$status);
			exit();
		}

	$row = sqlsrv_fetch_array( $RS_Headline01);
	$headline = $row[0];

?> 

Open in new window


Thanks for the help. :)
0
 
LVL 2

Author Closing Comment

by:splanton
ID: 40029596
Thank's Dave.
Still finding the transition from Classic ASP to PHP an 'interesting' experience.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40030047
You're welcome.  Classic ASP and PHP are similar enough to learn most of it fairly easily and different enough to keep tripping you up.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question