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 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形