dongpao5658
2017-11-15 00:21
浏览 45
已采纳

如何在PHP中将多个SQL SELECT结果组合成一个数组?

Hello this code works but currently it is very repetitive.

I have been trying for a few days now, and I just cannot work out how to rewrite these separate SELECTs into one loop using arrays.

I would like to push about 20 entries, so if I cannot work out how to do the loop then its going to messy! :(

I would be grateful for any help with the first loop and from that I will try and solve the rest myself from that advice, Thanks..

                // Create 6 random numbers from table
                $ran_num = (RandomInRange(1,$total,6));

                // Select data from table from a random row
                $grab1 = "select * from stock_tbl where stock_id='$ran_num[0]'";
                $grab2 = "select * from stock_tbl where stock_id='$ran_num[1]'";
                $grab3 = "select * from stock_tbl where stock_id='$ran_num[2]'";
                $grab4 = "select * from stock_tbl where stock_id='$ran_num[3]'";
                $grab5 = "select * from stock_tbl where stock_id='$ran_num[4]'";
                $grab6 = "select * from stock_tbl where stock_id='$ran_num[5]'";

                $result1 = $mysqli->query($grab1);
                $result2 = $mysqli->query($grab2);
                $result3 = $mysqli->query($grab3);
                $result4 = $mysqli->query($grab4);
                $result5 = $mysqli->query($grab5);
                $result6 = $mysqli->query($grab6);

                // Convert result into an array called items
                $item1 = mysqli_fetch_row($result1);
                $item2 = mysqli_fetch_row($result2);
                $item3 = mysqli_fetch_row($result3);
                $item4 = mysqli_fetch_row($result4);
                $item5 = mysqli_fetch_row($result5);
                $item6 = mysqli_fetch_row($result6);

I managed to solve this with help on this thread.. I replaced all this code with:

                // Create 6 random numbers from table
                $ran_num = (RandomInRange(1,$total,6));

                foreach ($ran_num as $key => $value) {
                  $grab[$key] = "select * from stock_tbl where stock_id='$value'";
                  $result = $mysqli->query($grab[$key]);
                  $item[$key] = mysqli_fetch_row($result);
                }

Thank you very much :)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongtou8736 2017-11-15 00:33
    已采纳
    <?php
        $ran_num = (RandomInRange(1,$total,6));
        foreach ($ran_num as $key => $value)
            {
                $grab[$value] = "select * from stock_tbl where stock_id='$value'";
            }
    ?>
    

    If you don't need to access each statement individually, this is also possible:

    <?php
        $ran_num = (RandomInRange(1,$total,6));
        $grab = '';
        foreach ($ran_num as $key => $value)
            {
                $grab .= "select * from stock_tbl where stock_id='$value';";
            }
    ?>
    

    That's essentially creating one long string that SQL will parse as individual commands using the semi-colon as the delimiter for each one.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongqianchi0512 2017-11-15 00:26

    You can concatenate strings in PHP with the '.' operator. Also, setting the grab variables as an array will make this easier as well, if that is possible in your implementation. You're probably looking for code such as:

    for (int i=0; i < 20; i++)
        $grab[i] = "select * from stock_tbl where stock_id='$ran_num[".i."]'";
    
    评论
    解决 无用
    打赏 举报
  • dongzhuo1930 2017-11-15 00:26

    First of all, do you really need *?

    What's wrong with using a single query with the IN() function? For example:

    // Create 6 random numbers from table
    $ran_num = (RandomInRange(1,$total,6));
    
    // Select(grab) data from table from a random row
    $grab = 'SELECT * FROM stock_tbl WHERE stock_id IN ('.implode(',', $ran_num).')';
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题