doujishan2247 2017-09-09 19:14
浏览 44
已采纳

bind_result和fetch返回null

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.

  • 写回答

2条回答 默认 最新

  • dsbezji539113152 2017-09-09 19:48
    关注

    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";
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存