With prepare statement i am fetching only one row , i tried while to loop all rows but is only one row witch is being fetched.please assist me on how i fetch all rows from database instead of one
PHP function : .....
public function StudentsOfParent($mobile){
$stmt = $this->conn->prepare("SELECT
a.id,
a.name,
a.mobile,
c.id as sutdentId,
c.user_id,
c.full_name,
c.school,
c.level,
c.year,
c.id
from users a
join students c
on a.id = c.user_id where a.mobile= ?");
$stmt->bind_param("i", $mobile);
if ($stmt->execute()) {
while ($user = $stmt->get_result()->fetch_assoc())
{
$stmt->close();
// return user's results
return $user;
}
}
else {
return NULL;
}
}
.....
External php file to access above function : retrieve.php:
<?php
include './DbHandler.php';
$db = new DbHandler();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['mobile'])){
$mobile = $_POST['mobile'];
$user = $db->StudentsOfParent($mobile);
if ($user != false) {
// user found successfully
$response["error"] = FALSE;
$response["user"]["id"] = $user["id"];
$response["user"]["sutdentId"] = $user["sutdentId"];
$response["user"]["user_id"] = $user["user_id"];
$response["user"]["full_name"] = $user["full_name"];
$response["user"]["school"] = $user["school"];
$response["user"]["level"] = $user["level"];
$response["user"]["year"] = $user["year"];
// $response["user"]["photo"] = $user["photo"];
echo json_encode($response);
// $json = json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Sorry we could not find you !";
echo json_encode($response);
}
}
else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter is missing!";
echo json_encode($response);
}
?>
$stmt->close();- You are "closing" the statement after the first loop.$stmt->close();but no changes$userin the loop.$stmt->close();,$userand i get this error :Fatal error: Call to a member function fetch_assoc() on boolean$stmt->get_result()->fetch_all(MYSQLI_ASSOC)