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?
 
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
 
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
 
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
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.

All Courses

From novice to tech pro — start learning today.