PHP MS SQL Stored Procedure dont return any value

We have a function that get the articleprice, when running this on Microsoft SQL it returns the correct value:
Eg: EXEC SP_GetCustomerProductPrice @ProductId = TESTITEM123, @CustomerId = 11444

But in the PHP it dont return anything... Any idea whats wrong?

public function sp_getCustomerPrice($prodNo, $custNo){
		require_once 'classes/validation.php';
		$validation = new validation;
		$customerPrice = 0;
		
		IF($validation -> validateProdNo($prodNo) AND $validation -> validateActNo($custNo)){
			$db_connection = new db_connection;	
			$conn = $db_connection -> connect();
			//$params = array(&$prodNo, $custNo);
			
			$ProductId = $prodNo;
			$CustomerId = $custNo;
			
			$params = array(
                array(&$ProductId, SQLSRV_PARAM_IN),
                array(&$CustomerId, SQLSRV_PARAM_IN)
            );
			
			//$tssql = "{CALL SP_GetCustomerProductPrice (?, ?)}";
			$tssql = "EXEC SP_GetCustomerProductPrice @ProductId = ?, @CustomerId = ?";
			
			$stmt = sqlsrv_query($conn, $tssql, $params);
			if($stmt){
				while($obj = sqlsrv_fetch_object($stmt)){
					//print $obj -> ArticleCustomerPrice;
					$customerPrice = $obj -> ArticleCustomerPrice;
				}
			}else{
				$customerPrice = 0;
			}
		}
		
		return $customerPrice;
}

Open in new window

LVL 1
myhrvoldAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
1. What do you see if you do a print_r($obj) inside your while() loop?

2. On a separate note, your while() loop implies that you expect to receive multiple rows in the result of your procedure, and you're basically taking the LAST result as the value you want to return (because each loop iteration is overwriting $customerPrice). You might want to either change your SP to select / return the last row (so the procedure becomes the definitive data source / model for the price and returns less data, instead of putting that logic into the client). Or if it only ever returns one row, then just do your fetch without the loop:


$customerPrice = 0;
if($stmt)
{
  if($obj = sqlsrv_fetch_object($stmt))
  {
    $customerPrice = $obj -> ArticleCustomerPrice;
  }
}
0
myhrvoldAuthor Commented:
Hi! The procedure always return only one row, so i removed the loop...

Still i dont get any result in the code, but if i exceute the query on the sql server.

The print_r($obj) ; Dont return anything....

public function sp_getCustomerPrice($prodNo, $custNo){
		require_once 'classes/validation.php';
		$validation = new validation;
		$customerPrice = 0;
		
		IF($validation -> validateProdNo($prodNo) AND $validation -> validateActNo($custNo)){
			$db_connection = new db_connection;	
			$conn = $db_connection -> connect();
			//$params = array(&$prodNo, $custNo);
			
			//$customerPrice = 2000;
			$ProductId = $prodNo;
			$CustomerId = $custNo;
			
			$params = array(
                array(&$ProductId, SQLSRV_PARAM_IN),
                array(&$CustomerId, SQLSRV_PARAM_IN)
            );
			
			//$tssql = "{CALL SP_GetCustomerProductPrice (?, ?)}";
			$tssql = "EXEC SP_GetCustomerProductPrice @ProductId = ?, @CustomerId = ?";
			
			$stmt = sqlsrv_query($conn, $tssql, $params);
			$customerPrice = 0;
			if($stmt)
			{
			  if($obj = sqlsrv_fetch_object($stmt))
			  {
				$customerPrice = $obj -> ArticleCustomerPrice;
			  }
			}		
		}
		return $customerPrice;
	}

Open in new window

0
myhrvoldAuthor Commented:
If i run the sql

EXEC SP_GetCustomerProductPrice @ProductId = TESTPROD1, @CustomerId = 11444

The result is:
ArticleCustomerPrice      ArticleNormalPrice      Agrement
385.900000                      454.000000                      1
0
myhrvoldAuthor Commented:
Hi! Found this on PHP.NET

When i added SET NOCOUNT ON in the procedure, the result is fine! Thanks for the help!

Attention!
If the sql contains INSERT, UPDATE or DELETE statements, the number of affected rows must be consumed. The sqlsrv_query returns a sql cursor that must be read to finish the transaction, if the result is non false. This same is valid for sqlsrv_execute. In this case the cursor must be also read using the prepared statement handle $smt.

Another solution is to place SET NOCOUNT ON at the top of the sqlsrv statement and all called procedures, functions and triggers.

We've practically observed it with sql statement with 500 inserts but only 368 was inserted without false returned. Prefixing by SET NOCOUNT ON or reading a cursor all rows were inserted.

See Processing Results (ODBC): https://docs.microsoft.com/en-us/sql/relational-databases/native-client-odbc-results/processing-results-odbc Each INSERT, UPDATE, and DELETE statement returns a result set containing only the number of rows affected by the modification. This count is made available when application calls SQLRowCount. ODBC 3.x applications must either call SQLRowCount to retrieve the result set or SQLMoreResults to cancel it. When an application executes a batch or stored procedure containing multiple INSERT, UPDATE, or DELETE statements, the result set from each modification statement must be processed using SQLRowCount or cancelled using SQLMoreResults. These counts can be cancelled by including a SET NOCOUNT ON statement in the batch or stored procedure.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
I appreciate the points, but my comment wasn't really the answer here, so I'm going to unmark it and change the answer to your last comment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.