PHP / MySQL:获取一个结果

This is my database:

database

This is the query:

SELECT * FROM users

Now when I do this:

$query = $connection->query($_GET['query']); // SELECT * FROM users
print_r($query->fetch_assoc());

I get this as output:

Array ( [id] => 3 [username] => karel )

Why does it not output id 4 and username ccscs?

When I a while loop:

whileloop

while($row = $query->fetch_assoc()){
    print_r($row);
}

This is happening because you don't give any order to your query so it automatically get first record. If you want to return last record you can order by id desc as follow

SELECT * FROM users ORDER BY id DESC

If you instead need to retrieve all records you will need to loop throw your records

while($row = $query->fetch_assoc())
{
    print_r($row);
}

Based on new op info i would not fetch twice but one as follow

$fields = array();
while($row = $query->fetch_assoc())
{
    print_r($row);
    $fields[] = $row;
}

That's because fetch_assoc() just fetches one row.

To get each row you can do it like this:

while($row = $query->fetch_assoc()){
   print_r($row);
}

fetch_assoc() retrieves a single row from a result set. See here.

mysqli_result::fetch_assoc -- mysqli_fetch_assoc — Fetch a result row as an associative array

You should use something like this :

while($row = $query->fetch_assoc()){
   print_r($row);
}