1

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);
   }
  ?>
5
  • 1
    $stmt->close(); - You are "closing" the statement after the first loop. Commented Jul 9, 2017 at 14:00
  • i removed $stmt->close(); but no changes Commented Jul 9, 2017 at 14:01
  • 1
    You also shouldn't return the $user in the loop. Commented Jul 9, 2017 at 14:02
  • I removed $stmt->close(); , $user and i get this error : Fatal error: Call to a member function fetch_assoc() on boolean Commented Jul 9, 2017 at 14:05
  • Just skip the while loop and use $stmt->get_result()->fetch_all(MYSQLI_ASSOC) Commented Jul 9, 2017 at 14:10

1 Answer 1

2

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.

That's because you're returning $user in the first iteration of while loop itself, the loop won't even go on for the 2nd iteration. Plus, you're also closing the statement object $stmt->close(); in the first iteration itself. Instead your code block should be like this:

// your code
$stmt->execute();
$result = $stmt->get_result();
$usersArr = array();
while ($user = $result->fetch_assoc()){
    $usersArr[] = $user;
}
return $usersArr;

Now the returned $usersArr array is a multidimensional array, which you need to appropriately loop through to get all users' details. If you want to see the complete array structure, do var_dump($usersArr);.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it seems that this solution is going to work for me but i get an error which shows that all variables in retrieve.php are undefined from id to year help me to check why , i changed $response["user"]["id"] = $user["id"] to $response["usersArr"]["id"] = $usersArr["id"] but doesn't work too
@gramandagrandish As I said in my answer, you need to loop through $usersArr array to get all users' details. Here's the solution snippet for your reference: https://pastebin.com/QhMLXNux

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.