0

I am using following php code to select a max value from a table in MS sql server database. This is just a snapshot of the code and not full code:

$sqlToCheckNID ="Select (?)=max(nid) from testRetailerlist";
$param_nid = array($maxNid,SQLSRV_PARAM_OUT);   
$maxNidInDb = sqlsrv_query($conn,$sqlToCheckNID,$param_nid); 
  echo "<li>" .$maxNid. "<li>";

Its throwing me error as Undefined variable maxNid

I want to echo the value that i get from the select statement. I think I am using the wrong syntax but could not found any example on net.

user2129794
  • 2,388
  • 8
  • 33
  • 51
  • Try using isset. See this link http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Adrian Mar 16 '13 at 10:33

1 Answers1

1

You need to add your parameters' array as a third argument to sqlsrv_query(). You should also pass the output parameters by reference after initializing them. So your your code would be like this:

$maxNid = 0;
$sqlToCheckNID = "SELECT (?)=MAX(nid) FROM testRetailerlist";
$param_nid = array(&$maxNid, SQLSRV_PARAM_OUT);   
$maxNidInDb = sqlsrv_query($conn, $sqlToCheckNID, $param_nid);
echo "<li>" .$maxNid. "<li>";

For more information please consult the documentation.

Adi
  • 5,089
  • 6
  • 33
  • 47