How can the dilemma of while be improved to just one query?

$product_data = $mysqli->query(
        "SELECT
          p.url as url,
          p.description as description,
          p.info as info,
          p.note as note,
          p.content as content,
          p.name as pname,
          p.icon as icon,
          p.price as price,
          p.original_price as original_price,
          p.prod_id as prod_id,
          p.stock as stock,
          p.views as views,
          s.name as sname,
          c.name as cname,
          p.add_time as add_time
        FROM `product` as p
        JOIN
          `store` as s ON p.store_id = s.store_id
        JOIN
          `category` as c ON c.category_id = p.category_id
        WHERE
          p.active = 1
        ORDER BY `prod_id` DESC
while($row = mysqli_fetch_array($product_data)){
$theme = $mysqli->query(
        "SELECT
        t.name as tname
        FROM `product` as p
        JOIN
          `product_theme` as pt ON p.prod_id = pt.prod_id
        JOIN
          `theme` as t ON t.theme_id = pt.theme_id
        WHERE
          p.prod_id = "{$row["prod_id"]}" "
      );

      $buyTotal = mysqli_fetch_array($mysqli->query(
        "SELECT sum(quan) as quan FROM `stock`
        WHERE
          `prod_id` = "{$row["prod_id"]}" "
      ));

      $viewsIp = mysqli_num_rows($mysqli->query(
        "SELECT * FROM `product_view`
        WHERE
          `prod_id` = "{$row["prod_id"]}" "
      ));

      $pre = mysqli_num_rows($mysqli->query(
        "SELECT sum(quan) as quan FROM `user_pre`
        WHERE
          `prod_id` = "{$row["prod_id"]}" "
      ));
}

Let"s take a look at my dilemma
while four queries all need $product_data $row ["prod_id"]
, and then these four queries will have other different functions
, such as showing that the conditions are available, printing the sum of a certain position

, and then these four queries will have other different functions, such as showing that the conditions are available, printing the sum of a certain bit

, and then these four queries will have other different functions

, for example, to show that the conditions meet the conditions, and to print the sum of a certain bit

.
<?=$pre;?>
<?=$buyTotal["quan"];?>

is done in this way.
but I hope that God can teach me, "I am civilized and grateful."

Apr.08,2021

Don't put the Sql statement in the loop, you can take out $row ['prod_id'] separately, and the sql statement can be written as wherein


your query can be completed in the following SQL statement

SELECT
  p.url                           AS url,
  p.description                   AS description,
  p.info                          AS info,
  p.note                          AS note,
  p.content                       AS content,
  p.name                          AS pname,
  p.icon                          AS icon,
  p.price                         AS price,
  p.original_price                AS original_price,
  p.prod_id                       AS prod_id,
  p.stock                         AS stock,
  p.views                         AS views,
  s.name                          AS sname,
  c.name                          AS cname,
  p.add_time                      AS add_time,
  t.name                          AS tname, --theme name
  (SELECT sum(quan)
   FROM `stock` AS sto
   WHERE sto.prod_id = p.prod_id) AS stock_quan, --Stockquan
  pv.*, --productView
  (SELECT sum(quan)
   FROM `user_pre` AS up
   WHERE up.prod_id = p.prod_id)  AS user_pre_quan --UserPrequan
FROM `product` AS p
  JOIN
  `store` AS s ON p.store_id = s.store_id
  JOIN
  `category` AS c ON c.category_id = p.category_id
  JOIN
  `product_theme` AS pt ON p.prod_id = pt.prod_id
  JOIN
  `theme` AS t ON t.theme_id = pt.theme_id
  JOIN `product_view` AS pv
    ON pv.prod_id = p.prod_id
WHERE
  p.active = 1
ORDER BY `prod_id` DESC
Menu