-1

我对自己做错了什么感到困惑。我已经在许多资源中查找了这一点,包括我正在学习 PHP 的书,它似乎应该是正确的......但它只是不起作用。

<?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';

这将返回第一个表(公园)中的所有数据就好了。项目commentname、comment、event、date 和description2 来自连接表(events 和comments)

如果我回显 '$row['state']' 我会得到正确答案。但是,如果我回显其他表中的任何项目(例如 $row['comment']),我不会得到任何结果。

我错过了什么?

4

3 回答 3

0

请试试这个:

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);
}
于 2012-10-20T21:55:11.177 回答
0

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

于 2012-10-20T22:51:29.740 回答
0

您没有编写查询来返回它们...

<?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);
}

或者您可以全部获取(请注意,如果表格中有列具有相同的名称,这将不起作用!):

    $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';
于 2012-10-20T21:52:21.317 回答