Q:

Best way to write function for SQL SERVER 2014

I am learning to write an function to fetch the user_status from SQL server.I generally use mysqli database and in the PHP i write code as :
function fetchuserStatus($username,$password){
  global $mysqli;
  $stmt = $mysqli->prepare("SELECT user_status from table_name where username = ? AND password = ?");
  $stmt->bind_param("ss",$username,$password);
  $stmt->execute();
  $stmt->bind_result($userstatus);
  $stmt->store_result();
  $stmt->fetch();
  $stmt->close();
  return $userstatus;
}

Now i want to write same for the SQL SERVER. How can i do that? As i did connection with SQL SERVER what would be the best way to write code for sql server.
NOTE: I am using PHP 7.1
Thanks in advance.

A:

Check out PHP sql server documentation http://php.net/manual/en/book.sqlsrv.php
But if we were to translate that to sql server.
function fetchuserStatus($username,$password){
  $conn = sqlsrv_connect('localhost', array("Database"=>"USERS_TBL", "UID"=>"dbuser", "PWD"=>"dbpassword");
  if ($conn) {
      $sql = "SELECT user_status from table_name where username = ? AND password = ?");
      $parameters = array($username,$password);
      $stmt = sqlsrv_query($conn, $sql, $parameters);
      if(sqlsrv_has_rows($stmt) === true) {
          sqlsrv_fetch($stmt);
          /*
           Use results however you want
           You can get results using sqlsrv_get_field($stmt, 0);
           0 is the index of the column on your select, if you selected something after user_status then that column would be 1 
          */
      }
      sqlsrv_free_stmt($stmt);
      sqlsrv_close($conn);
  }
}

