Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将mysql查询与子查询结果组合到一个PHP数组中_Php_Arrays - Fatal编程技术网

将mysql查询与子查询结果组合到一个PHP数组中

将mysql查询与子查询结果组合到一个PHP数组中,php,arrays,Php,Arrays,我有一个包含事件的表,要列出这些事件,我在事件表中循环,检查事件类型,并在特定的表中用它的eventId查找它的值 目前,它使用一个查询来获取20个事件,然后最多使用3个查询来获取这些事件的数据。我目前已经按程序对其进行了编码,但需要对其进行更改,以便在最后以数组形式返回数据 以下是我需要实现的PSEDOO代码示例: while(eventQuery): if commentQueryResult; $array .= commentQueryResult;

我有一个包含事件的表,要列出这些事件,我在事件表中循环,检查事件类型,并在特定的表中用它的eventId查找它的值

目前,它使用一个查询来获取20个事件,然后最多使用3个查询来获取这些事件的数据。我目前已经按程序对其进行了编码,但需要对其进行更改,以便在最后以数组形式返回数据

以下是我需要实现的PSEDOO代码示例:

while(eventQuery):

    if commentQueryResult;
        $array .= commentQueryResult;

    if forumPostQueryResult;
        $array .= forumPostQueryResult;

    if uploadItemQueryResult;
        $array .= uploadItemQueryResult;

endwhile;

return $array; // Combined returned results as one array
然后,我将能够访问数据并通过它进行每个循环

我只是不确定将多个结果集合并到一个数组中的最佳方法是什么

或者您可以尝试将它们组合成一个查询…

$eventResult = mysql_query(
    'SELECT userevent.event, userevent.eventId, userevent.friendId 
    FROM userevent
    WHERE userevent.userId = 1 OR userevent.friendId = 1 
    ORDER BY userevent.id DESC
    LIMIT 20'
);

while ($eventRow = mysql_fetch_row($eventResult)){

    if($eventRow[0] == 1){

        $result = mysql_fetch_array(mysql_query("
            SELECT forumRooms.id, forumRooms.title                                                                                       
            FROM forumPosts                                                 
            INNER JOIN forumRooms ON forumPosts.`forumTopic` = forumRooms.`id`                                                      
            WHERE forumPosts.id = '$eventRow[1]'"));
    }
    elseif($eventRow[0] == 2){

        $result = mysql_fetch_array(mysql_query("
            SELECT game.id, game.uriTitle, game.title                                                           
            FROM usergamecomment 
            INNER JOIN game ON usergamecomment.`gameId` = game.id
            WHERE usergamecomment.id = $eventRow[1]"));   
    }
    elseif($eventRow[0] == 4){

        $result = mysql_fetch_array(mysql_query("
            SELECT usercomment.comment, UNIX_TIMESTAMP(usercomment.TIME), `user`.id, `user`.username, `user`.activate
            FROM usercomment
            INNER JOIN `user` ON usercomment.`userId` = `user`.id
            WHERE usercomment.id = $eventRow[1]
            AND `user`.activate = 1"));
    }
    elseif($eventRow[0] == 5){

        $result = mysql_fetch_array(mysql_query("
            SELECT game.id, game.title, game.uriTitle 
            FROM game 
            WHERE game.id = $eventRow[1]"));
    }

// Combined Results as array
}
我正在将所有这些都转换为PDO,这是在找出最佳方法将其最小化后的下一步。

接受挑战。;)

因为您实际上只对while循环中的结果感兴趣,所以可以尝试这个查询。由于左连接,它可能不会更快,这在很大程度上取决于您的数据库。最终的
$result
包含所有元素及其各自的字段

$result = array();
$q = 'SELECT userevent.event AS userevent_event, 
      userevent.eventId AS userevent_eventId, 
      userevent.friendId AS userevent_friendId,
      forumRooms.id AS forumRooms_id,
      forumRooms.title AS forumRooms_title,
      game.id AS game_id,
      game.uriTitle AS game_uriTitle,
      game.title AS game_title,
      usercomment.comment AS usercomment_comment, 
      UNIX_TIMESTAMP(usercomment.TIME) AS usercomment_time,
      user.id AS user_id, 
      user.username AS user_username, 
      user.activate AS user_activate,
      g2.id AS game2_id,
      g2.uriTitle AS game2_uriTitle,
      g2.title AS game2_title


    FROM userevent
    LEFT JOIN forumPosts ON forumPosts.id = userevent.eventId
    LEFT JOIN forumRooms ON forumPosts.forumTopic = forumRooms.id
    LEFT JOIN usergamecomment ON usergamecomment.id = userevent.eventId
    LEFT JOIN game ON usergamecomment.gameId = game.id
    LEFT JOIN usercomment ON usercomment.id = userevent.eventId
    LEFT JOIN user ON usercomment.userId = user.id
    LEFT JOIN game g2 ON userevent.eventId = g2.id
    WHERE (userevent.userId = 1 OR userevent.friendId = 1)
      AND userevent.eventId >= (SELECT userevent.eventId 
                WHERE userevent.userId = 1 OR userevent.friendId = 1 
                ORDER BY userevent.id DESC LIMIT 1,20);';

$r = mysql_query($q);

while ( $o = mysql_fetch_row($r) ) {
  switch($o['userevent_event']) {
    case 1: 
      $result[] = array(
    'id' => $o['forumsRooms_id'],
    'title' => $o['forumsRooms_title'],
      );
      break;
    case 2: 
      $result[] = array(
    'id' => $o['game_id'],
    'uriTitle' => $o['game_uriTitle'],
    'title' => $o['game_title'],
      );
      break;
    case 4: 
      $result[] = array(
    'comment' => $o['usercomment_comment'],
    'time' => $o['usercomment_time'],
    'id' => $o['user_id'],
    'username' => $o['user_username'],
    'activate' => $o['user_activate'],
      );
      break;
    case 5: 
      $result[] = array(
    'id' => $o['game2_id'],
    'uriTitle' => $o['game2_uriTitle'],
    'title' => $o['game2_title'],
      );
      break;
  }
}
注意:最终,查询需要稍微编辑一下,我只是在没有测试的情况下写下了这一点。如果我对db结构有更多的了解,那么优化肯定可以完成


此外,这仅仅是一个概念证明,它确实可以用一个查询来完成

是否不可能通过连接表,通过单个查询同时查询事件列表和事件类型表?类似于:
SELECT*FROM event e internal JOIN eventType et ON e.eventID=et.eventID
虽然我也完全可能误解了你的问题。不,连接太复杂了,它使用php循环第一个查询,生成从其他结果返回的数据数组。太复杂了吗?我想试试我们。至少我喜欢脑筋急转弯@保罗问题更新,相当复杂的任务,加入他们作为一个查询?非常令人印象深刻!这将删除20个查询,运行只需0.7秒,因此外键必须工作正常。