YosiFZ
YosiFZ

Reputation: 7900

Php MySql Select count with prepared statment return always 1

I'm using this method to get make a login Web Service:

function redeem() {
    if (isset($_POST["user"]) && isset($_POST["pass"]) && isset($_POST["computer"])) {
        $user = $_POST['user'];
        $pass = $_POST['pass'];
        $computer = $_POST['computer'];

        $galNumb = "SELECT COUNT(*) FROM Useres WHERE username = ? AND password = ?";
        $stmt = $this->db->prepare($galNumb);
        $stmt->bind_param('ss', $user, $pass);
        $gNumb = $stmt->execute();

        $result = array(
            "success" => "true",
        );
        $this->sendResponse(200, $gNumb);
        return true;
    }
    $this->sendResponse(400, 'Invalid request');
    return false;
}

The problem is that $gNumb always return 1 even when the sql table not contain the username and the password. Any idea what can be the problem?

Upvotes: 0

Views: 54

Answers (1)

Alex
Alex

Reputation: 17289

You forgot to fetch results:

...
 $stmt->bind_param('ss', $user, $pass);
 if ($stmt->execute()) {
      $stmt->bind_result($gNumb);
      $stmt->fetch();
 } else {
      $gNumb = 0;
 }
...

Upvotes: 1

Related Questions