Mike
Mike

Reputation: 3

How to combine multiple SQL SELECT results into one array in PHP?

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 :)

Upvotes: 0

Views: 1077

Answers (3)

Payden K. Pringle
Payden K. Pringle

Reputation: 61

<?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.

Upvotes: 1

BenM
BenM

Reputation: 53198

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).')';

Upvotes: 2

Alex DeCamillo
Alex DeCamillo

Reputation: 355

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."]'";

Upvotes: 1

Related Questions