This question already has an answer here:
i am new to php... i got some error but cannot find it please help me.. error message....
Notice: Undefined variable: stmt.... Fatal error: Call to a member function bind_param() on null...
<?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);
?>
</div>
Change the variable $statement
to $stmt
you make mistake in variable name used same name of variable. other thinks
in bind_result bind result not used this query ('SELECT * FROM ...')
if you want used bind_result use this sync
$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);
or used
$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);