Same Id but multiple different product in SQL. Data should be retrieved from SQL and Output should be shortened to one line for each ID using PHP.

EXAMPLE:

SQL
ID Product
001 Laptop
001 Monitor
001 Speaker
002 Phone
003 Other Services

Expected Output PHP
ID : 001 Product: Laptop, Monitor, Speaker ID : 002 Product: Phone, Case ID : 003 Product: Other Services

MY CODE

$sql = "SELECT id, product From Stock";
$result = mysqli_query($conn, $sql);

while ($row = $result->fetch_array()){
$id[] = $row["id"];
$product[] = $row["product"];
}

$max_id = count($id);
$duplicate_id = array();

for($i=0; $i<$max_id;$i++){

$duplicate_m[$id[$i]] = $id[$i] = $product[$i];
}
print_r($duplicate_m);

Current Output

[001] => Laptop
[002] => Phone
[003] => Other Services

Refrence: https://www.interviewbit.com/sql-mcq/