3

Hi I am currently developing a php script that will simply return the value of one field. The code below is what ive been using

$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 is generating an output it is however always " 1 ". Im gussing this has something to do with how I am returning the value with sqlsrv_fetch however I am not sure what I should be using if this is the problem.

Thanks for any help givin :)

1 Answer 1

1

sqlsrv_fetch "Makes the next row in a result set available for reading." The response you are getting is "true" meaning that the operation has succeeded. What you want to do is use one of the variants of this command to get the data (code is not tested 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;

Read more about sqlsrv_fetch at php.net.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is essentially correct, with one correction: The sqlsrv_get_field function is meant to be used in conjunction with sqlsrv_fetch. i.e. Call sqlsrv_fetch to make a row ready for reading, then call sqlsrv_get_field to get the fields (one at a time) in the row. sqlsrv_fetch_array and sqlsrv_fech_object will fetch an entire row and can't be called after sqlsrv_fetch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.