0

I wrote a function to fetch the user_status from SQL Server but its seems everything is correct but still I am not getting any output.

My connection with SQL Server is established. And my function is as follows:

function fetchuserStatus($username,$password) {
    include("db-settings.php");   
    if ($conn) {
        $sql = "SELECT User_status from _yb_all_users_tbl where User_name = ? AND Password = ?";
        $parameters = array($username,$password);
        $stmt = sqlsrv_query($conn, $sql, $parameters);
        if( $stmt === false ) {
          die( print_r( sqlsrv_errors(), true));
         }
        if(sqlsrv_has_rows($stmt) === true) {
            sqlsrv_fetch($stmt);  
        }
        sqlsrv_free_stmt($stmt);
        sqlsrv_close($conn);
    } else {
        echo "Connection is not established";
    }
}
echo fetchuserStatus($username,$password);

And my db-settings.php file is:

$serverName = ".";
$connectionOptions = array(
    "Database" => "yaadbook",
    "UID" => "sa",
    "PWD" => "shiftu"
);
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if($conn) {
} else {
    echo "Connection Failure";
    die(print_r(sqlsrv_errors(),TRUE));
}

I am using PHP version 7.1.12. How can I resolve this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
Nikita Agrawal
  • 235
  • 1
  • 11

1 Answers1

0

I am not getting output because I need to use sqlsrv_execute() after sqlsrv_prepare().

Here is my complete code:

function fetchuserStatus($username,$password){ 
  include("db-settings.php");   
  $sql = "SELECT * from _yb_all_users_tbl where User_name = ? AND Password = ?";
  $parameters = array($username,$password);
  $stmt = sqlsrv_prepare($conn, $sql, $parameters);
  sqlsrv_execute($stmt); 
  if(!$stmt) {
      die( print_r( sqlsrv_errors(), true));
  }
  while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
     return $row;
  }
  sqlsrv_free_stmt($stmt);
  sqlsrv_close($conn);
} 
halfer
  • 19,824
  • 17
  • 99
  • 186
Nikita Agrawal
  • 235
  • 1
  • 11