PHP和Mysql - 嵌套循环

I have 2 tables - products and productimages. product has unique id and title. productimages has productid and imageurl. these are examples of my tables:

products:

|id|title    |
_____________
|1 |Laptop   |
|2 |Speakers |

productimages:

|productid|imageurl|
___________________
|    1    |lap1.png|
|    1    |lap2.png|
|    1    |lap3.png|
|    2    |spe1.png|

Right now I have a nested loop in PHP. loop through all rows of -> select * from products and for every product inside the loop -> select * from productimages where productid = id which is basically another loop inside the first loop.

and then I take all productimages into array and decode to JSON [title,photos].

Now imagine you have 2 million rows in productimages, the query times are too high, is there any way to make it more efficient?

$query = "SELECT * FROM products ORDER BY id LIMIT 10;
$result = mysqli_query($con,$query);

if(mysqli_num_rows($result)>0)
{
    $response = array();
    while($row = mysqli_fetch_assoc($result)) {
        $photos = array();
        $id = $row["id"];
        $title = $row["title"];         

        $queryp = "select imageurl from productimages where productid= '".$id."';";
        $resultp = mysqli_query($con,$queryp);

        if(mysqli_num_rows($resultp)>0)
        {
            while($row2 = mysqli_fetch_assoc($resultp)) {
                $photourl = $row2["imageurl"];
                array_push($photos,$photourl);
            }
        }
     }
  }

Some betterment for you could be:

1) Don't use select *. Use column names instead. e.g. select products.id, products.title, productimages.imageurl

2) Use JOIN instead of nested loop

So, you can try querying data like:

select products.id, products.title, productimages.imageurl
from products
join productimages on products.id = productimages.productid
ORDER BY products.id LIMIT 10

This case is not uncommon - you have two tables in a one to many relationship.

You should never nest an SQL call in a loop if you can possibly avoid it but there is a decision to be made about one SQL call or two.

A single SQL call could be:

SELECT id, title, imageURL
  FROM products LEFT JOIN productImages ON id=productid

The disadvantage of this is that you are extracting the title several times for each product and this is wasteful.

Using two SQL statements you can download the titles once for each product:

SELECT id, title FROM products

The results of this query can be stored in an associative array - so that you can look up the title for each id.

The second query is:

SELECT productid, imageURL FROM productImages ORDER BY productid, imageURL

You can loop through the results of this query, spitting out the title as you go.

To save the images with product you can add a column imageurl in the products table.collect the image names with , and insert that image name string to the products table. your table looks like below.

 +--------------+--------------+---------------------------+
 |     id       |    title     | imageurl                  |
 +--------------+--------------+---------------------------+
 |      1       |  Laptop      | lap1.png,lap2.png,lap3.png|
 +--------------+--------------+---------------------------+
 |      2       |  Speakers    | spe1.png                  |

Hope you understood what i explain.