如何用 bind_result 修复 get_result 和 fetch_assoc 并获取(不获取 Null)?
how to fix get_result and fetch_assoc with bind_result and fetch(wtithout getting Null)?
我正在一个基于教程的项目上工作,看起来我的主机不能完全支持 mysqlnd 使用 get_result。所以在我做了一些研究之后,我找到了使用 bind_result 的解决方案,几个小时后我试图修复它但仍然一无所获。
请大家帮我找出问题所在或者改正一下。
这是基于教程的代码,它们与 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 问题
我的天啊,差不多几个小时后,我通过更改为以下内容来修复它:
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;
}
我正在一个基于教程的项目上工作,看起来我的主机不能完全支持 mysqlnd 使用 get_result。所以在我做了一些研究之后,我找到了使用 bind_result 的解决方案,几个小时后我试图修复它但仍然一无所获。
请大家帮我找出问题所在或者改正一下。
这是基于教程的代码,它们与 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 问题
我的天啊,差不多几个小时后,我通过更改为以下内容来修复它:
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;
}