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
SELECT parks.id,
parks.state,
parks.name AS park_name, -- use an alias when column names exist in more than one table
parks.description,
parks.site,
parks.sname,
parks.street,
parks.city,
parks.zip,
parks.phone,
comments.comment, -- to return the comment from the comments table
events.name AS event_name, -- return name from the event table
event_date -- return date from the event table
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'
;
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
You didn't write the query to return them...
<?php
try
{
$sql = 'SELECT parks.id, parks.state, parks.name, parks.description, parks.site, parks.sname, parks.street, parks.city, parks.zip, parks.phone
--insert here any other column names you want to have in PHP
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);
}
Or you could just get all (beware, if there are columns in the tabl;es that have the same name, this won't work!):
$sql = 'SELECT *
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';
Please try this:
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 INNER JOIN photos INNER JOIN events ON parks.parkid = comments.parkid and parks.parkid = photos.parkid and parks.parkid = events.parkid';
$result = $pdo->query($sql);
}