Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SQL server 2008 not returning expected results

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 :)

like image 621
user1298268 Avatar asked Mar 06 '26 07:03

user1298268


1 Answers

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.

like image 190
Chris Adams Avatar answered Mar 07 '26 21:03

Chris Adams