I am just wondering which way to go with a database query. I have started using PDO recently with mysql. I am writing a small script that checks for manufacturers and then it checks items against each manufacturer. I am stuck whether it would be quicker to place the items in an array and (only use 1 query) then as i loop for the manufacturer array use an array_count_values to get the item quantities or do a seperate query in the loop to count the items.
I have about 400 manufacturers and 70000 items at present.
my current code using array is :
$itemquery = $conn->query("SELECT manufacturer FROM item_info_short");
$itemquery->execute();
$itemrow = $itemquery->fetchall(PDO::FETCH_ASSOC);
foreach ($itemrow as $itkey => $itvalue) {
$items[] = $itvalue[manufacturer];
}
$it_qty = array_count_values($items);
and then for my loop :
$manu_query = $conn->query("SELECT manufacturer FROM manufacturers ORDER BY manufacturer");
while($rowsx = $manu_query->fetch(PDO::FETCH_ASSOC)){
$rowid = $rowsx[manufacturer];
$count = $it_qty[$rowid];
if($count == '') $count = 0;
echo "<option value=\"$rowsx[manufacturer]\">$rowsx[manufacturer] $count Items</option>";
}
As you can see i use 2 PDO queries altogether.
The other method would use 401 queries.
I am trying to see which method is best practise and/or quicker.
Thanks in Advance for any advice.
To get count of manufacturers with cout of their goods, you have to make SQL to count them for you
will return all manufacturers with their goods count
if you want to get manufacturer details along – join this query with manufacturers table
if you need to list all manufacturers with their goods – use LEFT JOIN
something like this