0

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);
?>
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
kara
  • 123
  • 2
  • 9

2 Answers2

1

Change the variable $statement to $stmt

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Anish Joseph
  • 1,026
  • 3
  • 10
  • 24
0

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);
Abdu Hawi
  • 79
  • 2
  • 14