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

如何在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.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题