在我上传.php并在本地使用它之前,这是我的代码。
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();
}
我将.php上传到免费托管网站。运行后,我收到错误:调用未定义的方法mysqli_stmt :: get_result()
经过一番挖掘后,我找到了这个答案:stackoverflow.com
他们说我需要使用BIND_RESULT&取。但是我不知道在 while($ stmt-> fetch())块中写什么
所以,我一直在挖掘并找到了这个:stackoverflow.com
现在是我的代码:
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;
}
该函数由我的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";
}
}
因为我需要存储所有用户的信息。
但是在使用Postman后,我得到了这个结果。
{ “错误”:假, “ID”:NULL, “针”:NULL, “cardno”:空, “姓名”:空, “平衡”:NULL, “状态”:空} < /强>
它返回的所有内容都为空。
答案 0 :(得分:1)
n
将位于数字索引数组中。因此,您需要使用$user
代替$user[0]['id']
。
如果单个$user['id']
可以有多个记录,则还需要遍历cardno
数组(如果需要,也就是这样)。
答案 1 :(得分:0)
像这样改变上面的代码
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";
}