0

In $result should be all of users from datebase, but it takes only first person and shows error.

My php code:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    require_once 'connect.php';

    $response = mysqli_query($conn, "SELECT imie, nazwisko FROM users");

    $result = array();
    $result['osoby'] = array();

    $row = mysqli_fetch_assoc($response);

    $index['name'] = $row['imie'];
    $index['surname'] = $row['nazwisko'];

    array_push($result['osoby'], $index);
    $result['success'] = "1";

    echo json_encode($result);
}
1
  • 1
    use while loop Commented Jul 3, 2019 at 12:22

3 Answers 3

2

Its giving you 1 record because you are only printing 1 record,

Using $row = mysqli_fetch_assoc($response); will always give you last row if you not use loop here.

You need to use while loop to get all rows like:

<?php
$i = 0;
$result = array(); // initialize 
while($row = mysqli_fetch_assoc($response)){
    $result[$i]['name'] = $row['imie'];
    $result[$i]['surname'] = $row['nazwisko'];  // store in an array
    $i++;
}
$finalResult['osoby'] = $result; // storing as you need
$finalResult['success'] = "1"; // no idea about this but adding this also
echo json_encode($finalResult); // encode with json
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Your code is wrong, the result[$i]['name'] is going to get rewrite always because you are not incrementing it... at the end you are going to get only 1 result....
@Vidal: ohh silly forgot this. thanks
2

You can loop the result-set and append an array of your values to the $result array.

$response = mysqli_query($conn, "SELECT imie, nazwisko FROM users");

$result = ['osoby' => []];
while ($row = mysqli_fetch_assoc($response)) {
    $result['osoby'][] = ['name' => $row['imie'], 'surname' => $row['nazwisko']];
}
$result['success'] = "1";

echo json_encode($result);

If you have the mysqlnd driver installed, you can also use mysqli_result::fetch_all() method

$response = mysqli_query($conn, "SELECT imie, nazwisko FROM users");

$result = ['osoby' => mysqli_fetch_all($response, MYSQLI_ASSOC)];
$result['success'] = "1";

echo json_encode($result);

3 Comments

$result = ['osoby' => mysqli_fetch_all($response, MYSQLI_ASSOC)]; like this approach :)
Its quite neat indeed! Requires the mysqlnd driver though, but that does other cool stuff as well (like mysqli_stmt::get_result() for prepared statements).
yes agreed with u
2

You have to loop the result array.

$resultJson = array();
$resultJson['osoby']=array()

$query = "SELECT imie,nazwisko  FROM users";
$result = $mysql->query( $query );
if ($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
      // fetch information out of the $row..
      $resultJson['osoby'][]  = ['name' => $row['imie'], 'surname' => $row['nazwisko']];
    }

}

print json_encode($resultJson);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.