SQL从三个表中选择并保存在PHP数组中

时间:2011-12-01 11:03:55

标签: php sql

我正在开发一个Web应用程序,并希望稍微改进我的代码。

这是我为应用程序创建的SQL表结构:

enter image description here

现在我的问题:在PHP中是否有一种简单的方法可以选择(使用连接或其他什么?)来自所有表上的一个特定event_id的信息,并将它们保存在具有以下结构的数组中:

$events = array(
    'event_id' => 1,
    'event_name' => '{"de":"Weltwirtschaftsforum","fr":"Forum \u00e9conomique mondial","it":"Forum Economico Mondiale"}',
    'event_description' => '{"de":"Description DE","fr":"Description FR","it":"Description IT"}',
    'event_lastedit' => 2011-12-01 10:23:35,
    'locations' => array(
        array(
            'location_id' => 1,
            'location_name' => 'Bern',
            'location_date' => '01.01.2012',
            'location_deadline' => 1323340607,
            'timestamps' => array(
                array(
                    'timestamp_id' => 1,
                    'timestamp_time' => '10:30',
                    'timestamp_seats' => 30
                ),
                array(
                    'timestamp_id' => 2,
                    'timestamp_time' => '16:30',
                    'timestamp_seats' => 40
                )
            )
        ),
        array(
            'location_id' => 2,
            'location_name' => 'Davos',
            'location_date' => '02.02.2012',
            'location_deadline' => 1323340607,
            'timestamps' => array(
                array(
                    'timestamp_id' => 3,
                    'timestamp_time' => '12:30',
                    'timestamp_seats' => 50
                ),
                array(
                    'timestamp_id' => 4,
                    'timestamp_time' => '15:30',
                    'timestamp_seats' => 60
                )
            )
        )
    )
);

我希望这个问题很清楚。

问候 蜘蛛

编辑: 到目前为止我做了什么:

$event = $event_db->querySingle("SELECT * FROM rf_events WHERE event_id={$event_id}", true)

$rf_locations = $event_db->query("SELECT * FROM rf_locations WHERE event_id={$event_id}");

$locations = array();
$timestamps = array();
$counter = 0;

// Loop sql query result and save entries in $events array.
while ( $row = $rf_locations->fetchArray() ){

    $locations[$counter] = array(
        'location_id' => $row['location_id'],
        'location_name' => json_decode($row['location_name'], true),
        'location_date' => $row['location_date'],
        'location_deadline' => $row['location_deadline']
    );

    $rf_timestamps = $event_db->query("SELECT * FROM rf_timestamps WHERE location_id={$row['location_id']}");

    $counter2 = 0;

    while ( $row2 = $rf_timestamps->fetchArray() ){

        $locations[$counter]['timestamps'][$counter2] = array(
            'timestamp_id' => $row2['timestamp_id'],
            'timestamp_time' => $row2['timestamp_time'],
            'timestamp_seats' => $row2['timestamp_seats']
        );

        $counter2++;
    }

    $counter++;
}

1 个答案:

答案 0 :(得分:0)

SELECT * FROM rf_events, rf_locations, rf_timestamps WHERE  
rf_locations.event_id = rf_events.event_id AND 
rf_timestamps.location_id = rf_locations.event_id

然后,使用相应的数据添加到php中。

您可以参考:MYSQL JOIN