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

如何在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条)

报告相同问题?

悬赏问题

  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件