[英]how to fix get_result and fetch_assoc with bind_result and fetch(wtithout getting Null)?
i'm working on a project base on a tutorial, looks like my host can't fully support mysqlnd for using get_result. 我正在基于教程的项目上工作,看来我的主持人不能完全支持mysqlnd使用get_result。 so after i've did some researches, i've found solution in using bind_result, after hours i've tried to fix it and still nothing.
因此,在进行了一些研究之后,我找到了使用bind_result的解决方案,数小时后我试图对其进行修复,但仍然一无所获。
please help me to find the problem or change it in proper way. 请帮助我找到问题或以适当的方式进行更改。
Here are the Codes base on Tutorial which they worked fine with XAMPP : 这是基于Tutorial的代码,它们可以与XAMPP很好地配合使用:
Function.php 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;
}
And After I've changed them to this in function.php 在我将它们更改为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;
}
and now i'm getting 现在我正在
{"phone":null,
"name":null,
"birthdate":null,
"address":null,
"avatarUrl":null
}
instead of 代替
{"phone":"+18561523172",
"name":"Johb",
"birthdate":"1983-02-14",
"address":"Nxy 123",
"avatarUrl":""
}
thanks. 谢谢。
Edit 01. 编辑01。
to answer the questions about error, this is the error : 回答有关错误的问题,这是错误:
<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}
before this with get_result i had issue with 在此之前与get_result我有问题
Call to undefied method mysqli_stmt::get_result(
and the solution was to change it to bind_result and host don't care about mysqlnd problem 解决方案是将其更改为bind_result,主机不关心mysqlnd问题
omg After almost hours i've fixed it by changin to this : 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;
}
Solution was to change the code to this. 解决方案是将代码更改为此。 Thanks for Responds from @jereon , @RiggsFolly , NiggelRen .
感谢@ 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.