注意:未定义的变量:stmt

时间:2018-03-23 10:16:13

标签: php

我是php新手...我收到了一些错误但找不到它请帮助我.. 错误信息....

  

注意:未定义的变量:stmt ....致命错误:在null上调用成员函数bind_param()...

<?php
require "conn.php";

$username = $_POST["username"];
$password = $_POST["password"];


$statement = $conn->prepare("SELECT * FROM data WHERE username = ? AND password = ?");
$stmt->bind_param('ss' ,$username ,$password);
$stmt->execute();
$stmt->bind_result($username ,$password);
$stmt->store_result();

$response = array();
$response["success"] = false;  

while($stmt->fetch($statement)){
    $response["success"] = true;  
    $response["name"] = $name;
    $response["age"] = $age;
    $response["username"] = $username;
    $response["password"] = $password;
}

echo json_encode($response);
?>

2 个答案:

答案 0 :(得分:1)

将变量$statement更改为$stmt

答案 1 :(得分:0)

你在变量名中使用了相同名称的变量。 其他人认为
bind_result 中 绑定结果未使用此查询('SELECT * FROM ...')

如果您想使用bind_result,请使用此同步

$stmt = $conn->prepare("SELECT name,age FROM data WHERE username = ? AND password = ?");
$stmt->bind_param('ss' ,$username ,$password);
$stmt->execute();
$stmt->bind_result($name ,$age);

$response = array();  

while($stmt->fetch($stmt)){
    $response['success'] = true;  
    $response['name'] = $name;
    $response['age'] = $age;
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);

或使用

$stmt = $conn->prepare("SELECT * FROM data WHERE username = ? AND password = ?");
$stmt->bind_param('ss' ,$username ,$password);
$stmt->execute();
$result = $stmt->get_result();
$response = array();  

while($row = $result->fetch_assoc())){
    $response[] = $row;
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);