[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-04-28
7
Medium Priority
?
4,480 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 111

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 84

Accepted Solution

by:
Dave Baldwin earned 1800 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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 84

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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 setup several different housekeeping processes for a SQL Server.
Suggested Courses

649 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