Solved

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

Posted on 2014-04-28
7
3,971 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 110

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article discusses how to implement server side field validation and display customized error messages to the client.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

726 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