I am baffled as to what I am doing wrong. I’ve looked this up in many resources, including the book that I am learning PHP out of, and it seems like it should be right…but it is just not working.
<?php
try
{
$sql = 'SELECT parks.id, parks.state, parks.name, parks.description, parks.site, parks.sname, parks.street, parks.city, parks.zip, parks.phone FROM parks
INNER JOIN comments ON parks.parkid = comments.parkid
INNER JOIN photos ON parks.parkid = photos.parkid
INNER JOIN events ON parks.parkid = events.parkid';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching data: ' . $e->getMessage();
include 'output.html.php';
exit();
}
foreach ($result as $row)
{
$datas[] = array ('id' =>$row['id'],
'parkid' =>$row['parkid'],
'state' =>$row['state'],
'name' =>$row['name'],
'description' =>$row['description'],
'site' =>$row['site'],
'sname' =>$row['sname'],
'street' =>$row['street'],
'city' =>$row['city'],
'phone' =>$row['phone'],
'zip' =>$row['zip'],
'commentname' =>$row['commentname'],
'comment' =>$row['comment'],
'event' =>$row['event'],
'date' =>$row['date'],
'description2' =>$row['description2']);
}
include 'writing.html.php';
This returns all of the data that is in the first table (parks) just fine. The items commentname, comment, event, date, and description2 are from the joined tables (events and comments)
If I echo ‘$row[‘state’]’ I get a correct answer. But if I echo any of the items from the other tables (for instance $row[‘comment’]) I get no result.
What am I missing?
SQL showing how to return columns from the joined tables
This is not a complete solution, but a demonstration of ho to return data from the joined tables rather than purely from the main table