ターミナルに送り返すためにJSONに入れる必要があるやや複雑なphp sqlクエリがあります。誰か助けてくれませんか?私はこれに苦労しており、正しく理解できないようです。私のコードは次のようなものです:
//Grab all people living in a city, and for each of those people, grab all of their carsmodel and license plate. My output should be something like [{"firstname":John, "lastname":Smith, "cars":[car1, car2, car3...], "plates":[plate1, plate2, ...]},{"firstname":Jack,"lastname":Hide, "cars":[car1, car2, car3], "plates":[plate1, plate2,...]}]
$sql = "SELECT id, firstname, lastname FROM residents WHERE residents.city = ?";
$q = $connection->prepare($sql);
$q->execute(array($city));
while($row = $q->fetch())
{
$sql2 = "SELECT carid FROM cars WHERE userid = ?"
$q2 = $connection->prepare($sql2);
$q2->execute(array($row[0]));
while($row2 = $q2->fetch())
{
// What do I do here?!
}
}
return json_encode(//?);
どんな助けでも大歓迎です!
ありがとう!