PHP SQL Server 2008 not returning expected results

Hi I am currently developing a php script that will just return the value of one field. Below is the code that ive used

$code = $_POST['code'];

$serverName = "EDIT";
$connectionInfo = array("UID" => "EDIT", "PWD" => "EDIT", "Database"=>"EDIT");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

$tsql2 = "SELECT paitentno FROM paraappreg WHERE appno = $code";

$result2 = sqlsrv_query( $conn, $tsql2, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

  print sqlsrv_fetch($result2);

      

This code generates output, but it is always "1". It seems to me that it has something to do with the way I return the value using sqlsrv_fetch, but I'm not sure which I should use if this is the problem.

Thanks for any help givin :)

+3


source to share


1 answer


sqlsrv_fetch "Makes the next row in the result set readable." The answer you receive is "true", which means the operation succeeded. What you want to do is use one of the variations of this command to get data (code untested, but should work):

    print ($result2); // response: 1 ("true")

    print_r(sqlsrv_fetch_array($result2)); // response: Array("paitentno"=>'1234');
    print_r(sqlsrv_fetch_object($result2)); // response: StdObj->paitentno->1234;
    // this gets the item by index:
    print_r(sqlsrv_get_field($result2, 0)); // response: 1234;

      



More on sqlsrv_fetch at php.net.

+1


source







All Articles