如何使用bind_result和获取修复get_result和fetch_assoc(如果获得空)?



我正在根据教程开发一个项目,看起来我的主机无法完全支持 Mysqlnd 使用 get_result。 因此,在我做了一些研究之后,我找到了使用bind_result的解决方案,几个小时后我试图修复它,但仍然一无所获。

请帮助我找到问题或以正确的方式更改它。

以下是基于教程的代码,它们与XAMPP配合良好:

功能.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;
}

在我将它们更改为功能之后.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问题

天哪,将近几个小时后,我已经通过 changin 修复了它:

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;
}

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

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;
}

相关内容

  • 没有找到相关文章

最新更新