如何使用bind_result和fetch(不获取Null)修复get_result和fetch_assoc?

时间:2018-08-09 08:35:25

标签: php api null mysqlnd

我正在基于教程进行项目开发,看来我的主持人不能完全支持mysqlnd使用get_result。因此,在进行了一些研究之后,我找到了使用bind_result的解决方案,数小时后我尝试对其进行修复,但一无所获。

请帮助我发现问题或以适当的方式解决问题。

这是基于Tutorial的代码,它们可以与XAMPP一起很好地工作:

Function.php

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT * FROM User WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $user = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        return $user;
    } else
        return NULL;
    }

然后我在function.php中将它们更改为此。

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT Phone, Name, Birthdate, Address FROM User WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $stmt->bind_result($phone, $name, $birthdate, $address);
        $user = array();

        while($stmt->fetch()) {
            $tmp = array();
            $tmp["phone"] = $phone;
            $tmp["name"] = $name;
            $tmp["birthdate"] = $birthdate;
            $tmp['address'] = $address;
            array_push($user, $tmp);
        }


        $stmt->close();

        return $user;
    } else
        return NULL;
    }

现在我要得到

{"phone":null,
"name":null,
"birthdate":null,
"address":null,
"avatarUrl":null
}

代替

{"phone":"+18561523172",
"name":"Johb",
"birthdate":"1983-02-14",
"address":"Nxy 123",
"avatarUrl":""
}

谢谢。

编辑01。

回答有关错误的问题,这就是错误:

<br />
<b>Notice</b>:  Undefined index: Phone in <b>/home/meskand1/public_html/pasargad-    drinkshop/getuser.php</b> on line <b>24</b><br />
<br />
<b>Notice</b>:  Undefined index: Name in <b>/home/meskand1/public_html/pasargad-    drinkshop/getuser.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>:  Undefined index: Birthdate in <b>/home/meskand1/public_html/pasargad-drinkshop/getuser.php</b> on line <b>26</b><br />
<br />
<b>Notice</b>:  Undefined index: Address in <b>/home/meskand1/public_html/pasargad-drinkshop/getuser.php</b> on line <b>27</b><br />
<br />
<b>Notice</b>:  Undefined index: avatarUrl in <b>/home/meskand1/public_html/pasargad-drinkshop/getuser.php</b> on line <b>28</b><br />
{"phone":null,"name":null,"birthdate":null,"address":null,"avatarUrl":null}

在使用get_result之前,我遇到了问题

Call to undefied method mysqli_stmt::get_result(

,解决方案是将其更改为bind_result,主机不关心mysqlnd问题

omg将近几个小时后,我通过更改它来修复它:

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT Phone, Name, Birthdate, Address FROM user WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $stmt->bind_result($arr['Phone'], $arr['Name'], $arr['Birthdate'], $arr['Address']);
        while ($stmt->fetch()) {
            $user[] = $arr;
        }
        $stmt->close();

        return $user;
    } else
        return NULL;
}

1 个答案:

答案 0 :(得分:2)

解决方案是将代码更改为此。感谢@jereon@RiggsFollyNiggelRen的回复。

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT Phone, Name, Birthdate, Address FROM user WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $stmt->bind_result($arr['Phone'], $arr['Name'], $arr['Birthdate'], $arr['Address']);
        while ($stmt->fetch()) {
            $user[] = $arr;
        }
        $stmt->close();

        return $user;
    } else
        return NULL;
}