简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM