0

This is my code before I upload my .php and use it locally.

        public function getUserByCardNo($cardno){
        $stmt = $this->con->prepare("SELECT * FROM accounts WHERE cardno = ?");
        $stmt->bind_param("s",$cardno);
        $stmt->execute();
        return $stmt->get_result()->fetch_assoc(); 
    }

I uploaded my .php in a free hosting site. After running, I get the error: Call to undefined method mysqli_stmt::get_result()

After some digging, I found this answer: stackoverflow.com

They said that I needed to use BIND_RESULT & FETCH. But I don't know what to write inside the while($stmt->fetch()) block

So, I keep digging and found this: stackoverflow.com

and here is my code now:

        public function getUserByCardNo($cardno){
        $stmt = $this->con->prepare("SELECT id, name, cardno, pin, balance, status FROM accounts WHERE cardno = ?");
        $stmt->bind_param("s",$cardno);
        $stmt->execute();
        $stmt->bind_result($id, $name, $cardno, $pin, $balance, $status);
        $info = array();
        while ($stmt->fetch()){
            $tmp = array();
            $tmp["id"] = $id;
            $tmp["name"] = $name;
            $tmp["cardno"] = $cardno;
            $tmp["pin"] = $pin;
            $tmp["balance"] = $balance;
            $tmp["status"] = $status;
            array_push($info, $tmp);
        }
        $stmt->close();
        return $info;
    }

That function is called by my userLogin.php

if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['cardno']) and isset($_POST['pin'])){
    $db = new DbOperations();
    if($db->userLogin($_POST['cardno'], $_POST['pin'])){
        $user = $db->getUserByCardNo($_POST['cardno']);
        $response['error'] = false;
        $response['id'] = $user['id'];
        $response['pin'] = $user['pin'];
        $response['cardno'] = $user['cardno'];
        $response['name'] = $user['name'];
        $response['balance'] = $user['balance'];
        $response['status'] = $user['status'];

    }
    else{
        $response['error'] = true;
        $response['message'] = "Invalid Card Number or Pin";
    }
}
else{
    $response['error'] = true;
    $response['message'] = "Required fields are missing";
}

}

Because I need to store all the user's info.

But after using Postman, im getting this result.

{"error":false,"id":null,"pin":null,"cardno":null,"name":null,"balance":null,"status":null}

Everything it return is null.

Ralph Ignacio
  • 43
  • 1
  • 8

2 Answers2

1

$user would be inside a numerically indexed array. So you would need to use $user[0]['id'] instead of $user['id'].

If a single cardno can have multiple records, you would need to iterate through $user array as well (if needed, that is).

raidenace
  • 12,380
  • 1
  • 27
  • 33
0

Change the above code like this

if($_SERVER['REQUEST_METHOD']=='POST'){
        if(isset($_POST['cardno']) and isset($_POST['pin'])){
            $db = new DbOperations();
            if($db->userLogin($_POST['cardno'], $_POST['pin'])){
                $user = $db->getUserByCardNo($_POST['cardno']);
                $response['error'] = false;
                $response['id'] = $user[0]['id'];
                $response['pin'] = $user[0]['pin'];
                $response['cardno'] = $user[0]['cardno'];
                $response['name'] = $user[0]['name'];
                $response['balance'] = $user[0]['balance'];
                $response['status'] = $user[0]['status'];

            }
            else{
                $response['error'] = true;
                $response['message'] = "Invalid Card Number or Pin";
            }
        }
        else{
            $response['error'] = true;
            $response['message'] = "Required fields are missing";
        }
Manzoor Samad
  • 870
  • 1
  • 8
  • 15