Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4831
  • Last Modified:

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

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
splanton
Asked:
splanton
  • 3
  • 2
  • 2
2 Solutions
 
Ray PaseurCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
splantonAuthor Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Ray PaseurCommented:
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
 
splantonAuthor Commented:
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
 
splantonAuthor Commented:
Thank's Dave.
Still finding the transition from Classic ASP to PHP an 'interesting' experience.
0
 
Dave BaldwinFixer of ProblemsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now