0

So, I have a table called employees

this table contains alot of information about employees, and I want to to turn each row into a JSON object.

$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
$employees = Array();

if ($result->num_rows > 0) {
  // output data of each row
  
  while($row = $result->fetch_assoc()) {
    // Here I want to somehow add the name and age into the $employees variable.
  }
} else {
  echo "0 results";
}

I want it to look something like this:

{
id: id of employee {
name: "name of employee",
age: "age of employee"
},
id: id of employee {
name: "name of employee",
age: "age of employee"
}
}
  • I think `json_encode()` is what you need, check it [here](https://www.php.net/manual/en/function.json-encode.php). – NcXNaV Jul 22 '21 at 16:29

0 Answers0