PHP:在数组中选择表值?

时间:2018-10-13 14:52:38

标签: php mysql database

我有两个表:“主题”和“用户”。

此处的代码(不是我写的)选择TOPICS表值并将其分配给数组:

$sql = 'SELECT forum_id, topic_id, topic_title, topic_time, topic_views, topic_poster, topic_posts_approved, topic_first_poster_name, topic_first_poster_colour, topic_last_post_id, topic_last_poster_name, topic_last_poster_colour, topic_last_post_time, topic_last_view_time, topic_last_poster_id
    FROM ' . TOPICS_TABLE . '
    WHERE ' . $this->db->sql_in_set('forum_id', $flast) . '
    AND ' . $this->content_visibility->get_visibility_sql('topic', 'topic') . '
    ORDER BY topic_last_post_time DESC';
$result = $this->db->sql_query_limit($sql, $this->config['last_total']);

while ($row = $this->db->sql_fetchrow($result))
{
$this->template->assign_block_vars('last_topic', array(
        'LAST_LINK'      => append_sid("{$this->phpbb_root_path}viewtopic.$this->phpEx", 'f=' . $row['forum_id'] . '&t=' . $row['topic_id']),
        'U_LAST_TOPIC'   => append_sid("{$this->phpbb_root_path}viewtopic.$this->phpEx", 'f=' . $row['forum_id'] . '&p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id']),
        'LAST_POSTER'     => append_sid("{$this->phpbb_root_path}memberlist.$this->phpEx", 'mode=viewprofile' . '&u=' . $row['topic_poster']),
        'USERNAME_LAST'  => append_sid("{$this->phpbb_root_path}memberlist.$this->phpEx", 'mode=viewprofile' . '&u=' . $row['topic_last_poster_id']),
        'TOPIC_TITLE'                   => $row['topic_title'],
        'TOPIC_VIEWS'                   => $row['topic_views'],
        'TOPIC_REPLIES'                 => $row['topic_posts_approved'],
        'TOPIC_LAST_POSTER_NAME'        => $row['topic_last_poster_name'],
        'TOPIC_LAST_POSTER_COLOUR'      => $row['topic_last_poster_colour'],
        'TOPIC_LAST_POST_TIME'          => $this->user->format_date($row['topic_last_post_time']),
        'TOPIC_LAST_VIEW_TIME'          => $this->user->format_date($row['topic_last_view_time']),
        'USERNAME_AV'                   => "test",
    ));
}
$this->db->sql_freeresult($result);

数组中的最后一个元素是“ USERNAME_AV”。我想从USERS表中分配数据,其中表行/用户ID = topic_last_poster_id(取自TOPICS表),但不确定是否可以在数组内完成。

欢迎提出任何建议!

1 个答案:

答案 0 :(得分:0)

我将通过联接users表来实现这一点,以便您可以在SQL级别上做到这一点。这提供了性能优势,因为您无需建立多个数据库连接。您所要做的就是在您选择的JOIN USERS ON (USERS.id = TOPICS.topic_last_poster_id);

上使用此语法

然后在原始文件中选择添加USER.USERNAME_AV as AV,就可以像访问其余数据一样访问AV。

所以应该是$row['av']

希望这有助于配合!干杯。