Solved

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

Posted on 2014-04-28
7
3,337 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 108

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 82

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 108

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 82

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now