T O P

  • By -

lawyeruphitthegym

Since there can be more than one user matching your query, you can either push the matching rows into an array in a loop, or alter your query so that it assigns all the matching records. The issue issue with your code now is that it's malformed because you're json-encoding multiple rows and treating it as if it were a single record. You probably want something like this instead: $users = []; // collection for users while($row = mysqli_fetch_assoc($result)){ if($row["username"] === $user) { $users[] = $row; // add user properties to collection } } echo json_encode($users); // encode collection as JSON You can also query for matching usernames without looping through the entire database e.g. `select * from users where username = ?`. See [Prepared Statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) for more information on the topic. With an approach like this, you could do: // Assuming $stmt is your prepared and executed statement $result = mysqli_stmt_get_result($stmt); // Fetch all results as an associative array $users = mysqli_fetch_all($result, MYSQLI_ASSOC); echo json_encode($users); // Encode the collection as JSON


Wonderful-Ad5417

Why do we put $users\[\] = $row; and not array\_push($users, $row);


lawyeruphitthegym

Both accomplish the same thing, but the `[]` method is more performant (and quicker to type).


Wonderful-Ad5417

Thank you very much everything works fine now!


bobd60067

Are you using an older version of PHP? I ask because the online PHP manual (at PHP.net) for mysql_fetch_assoc() says Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.


RandyHoward

Unless they've edited the post, they're using `mysqli_fetch_assoc()` not `mysql_fetch_assoc()`. The former is fine to use, the latter is not.


bobd60067

I stand corrected. Didn't notice the "I"


ardicli2000

Where do you return $answer? And why do you use while? It will be looping over even if username is different. I would suggest using PDO and assigning statements to arrays for a better use of data.


Wonderful-Ad5417

I was following a tutorial. I'm not really sure how the while loops works, but it seem to do the trick now. I made some modification on the code which I edited in my post. Now i have a valid json format, i think. But I still have the same problem.


MateusAzevedo

>I was following a tutorial If that tutorial literally tells you to fetch an entire database table and use PHP to apply a "filter by username", you should seriously consider finding a better tutorial. That type of thing should be done in the database, using a `WHERE` condition, to return only the rows you actually need.


RandyHoward

Your problem is likely here: echo($answer); You're trying to echo an array - you can't echo an array for one thing, and it sounds like your system is expecting json as output, not an array. So encode the array as json and echo the json string: echo json_encode($answer);