1

How to set a single query to loop on all tables in my MySql database?

Have tried this and got tables in print_r()

function landing() {
    $db_handle = new DBController;
    $games_table_array = $db_handle -> runQuery("SHOW TABLES LIKE '%games'");

    echo '<pre>';
    print_r($games_table_array);

}

I got all tables

    Array
(
    [0] => Array
        (
            [Tables_in_h2b (%games)] => apk_games
        )

    [1] => Array
        (
            [Tables_in_h2b (%games)] => pc_games
        )

    [2] => Array
        (
            [Tables_in_h2b (%games)] => psp_games
        )

)

But now I want to make a foreach loop again on each table but I'm confused on what to do

Tried this but

foreach ($games_table_array as $game_table) {
            $games_array = $db_handle -> runQuery("SELECT * FROM '$games_table_array[$game_table]['?SHOULD BE COLUMN NAME BUT IT'S A TABLE? WHAT TO WRITE HERE']' WHERE command > 20 ORDER BY command DESC LIMIT 10");
        
            foreach ($games_array as $key => $value) {
                ?>
                <div class="container">
                    <!-- //! To IMPLEMENT directly add to cart shop
                    <a href="home?t=games-pc">
                        <img src="assets/images/games_pc/<?php echo $games_array[$key]["img"]?>" alt="">
                    </a> -->
                    <img src="assets/images/games_pc/<?php echo $games_array[$key]["img"]?>" alt="">
                    <span><?php echo $games_array[$key]["command"]?> achats</span>
                </div>
                <?php 
            }
        }
Stu
  • 30,392
  • 6
  • 14
  • 33
  • 1
    `tried this but...`...but what? Explain the exact problem please. – ADyson Aug 17 '22 at 11:22
  • the problem is I don't know what to fetch the array – Mario Shaya Aug 17 '22 at 11:24
  • 1
    `foreach ($games_table_array as $game_table)`, and then inside trying to access `$games_table_array[$game_table]`, that makes no sense. `$game_table` already _is_ one of the elements of your `$games_table_array` at this point. Trying to use that as key / index, makes no sense whatsoever. – CBroe Aug 17 '22 at 12:02
  • Study the main answer here for how to understand your array structures, write the code correctly, and debug it as you go along: [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – ADyson Aug 17 '22 at 13:14
  • So we can't add foreach in a foreach? or what? – Mario Shaya Aug 17 '22 at 15:52
  • Of course you can. But if you read Cbroe's comment, and the link I gave, and study your data structure, you'll start to see why what you wrote doesn't really make much sense. – ADyson Aug 18 '22 at 12:57
  • And the issue isn't related to the inner foreach, I'm not sure why you thought that, from the explanations provided – ADyson Aug 18 '22 at 13:14

0 Answers0