Select query returning false on sql server in PHP

Do you have questions regarding other database enginges (not MySQL) -- ask here!

Moderators: egami, macek, gesf

Post Reply
phpbee
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jul 26, 2018 12:15 am

Thu Jul 26, 2018 12:18 am

I am using SQL(MSSQL) Server database on Windows Server. My database connection is working perfect. I am trying to get data using Select Query. Here is my code,

db_functions.php

Code: Select all

 public function login($username, $password)
 {

   $sqlString = "SELECT CompId , BUCode , Role from 
   PRIME_APPUSR WHERE UserName = ? AND PassWord = ?";

   $params = array($username, $password);

    $stmt = sqlsrv_query($this->conn, $sqlString, $params);
    if ($stmt === false) {

        die(print_r(sqlsrv_errors(), true));
    }

    $rows = sqlsrv_has_rows($stmt);

    if ($rows === true) {

        while ($row = sqlsrv_fetch($stmt)) {

            $response["CompId"] = sqlsrv_get_field($stmt, 0);
            $response["BUCode"] = sqlsrv_get_field($stmt, 1);
            $response["Role"]   = sqlsrv_get_field($stmt, 2)

        }

        return $response;

    } else {
        return false;
    }

}
login.php

Code: Select all

 <?php

  require_once 'db_functions.php';
  $db = new db_functions();

  if (isset($_POST['USERNAME']) && isset($_POST['PASSWORD'])) {

  $USERNAME = $_POST['USERNAME'];
  $PASSWORD = $_POST['PASSWORD'];

  $result1 = $db->login($USERNAME, $PASSWORD);

  if (!$result1) {

    $arr["response"] = "Error.";

  } else {

    $arr["response"] = $result1;

  }

  echo json_encode($arr);

  } else {

    $arr["response"] = "Invalid Parameters";
    echo json_encode($arr);

  }

?>
my question is , i am getting a false in return from this function. can anyone guide to solve this.?

Post Reply