Suppose there is a table "basket".

id_basket id_product id_user 

Where it is necessary to count the quantity of the largest ordered product.

id_product

 $sql = "SELECT count(*) FROM `basket` WHERE id_product= 3"; $result = $con->prepare($sql); $result->execute(); $number_of_rows = $result->fetchColumn(); 

For a particular product is clear how to count.

    1 answer 1

    If I understand correctly, the best-selling product is the one whose id id_product is found in the table most often. So we need to group by this field and find the count(id_product) . Then sort by quantity and display the first entry.

     $sql = "SELECT id_product, count(id_product) as cnt FROM `basket` GROUP BY id_product ORDER BY cnt limit 1"; 

    There may be errors in the dialect, but I think the meaning is clear.