Hi all,
Following on from some other threads, I'm trying to get to grips with the new ways of PDO, PHP and MySQL and I'm mathematically failing.
Basically, I have a table with a column called type_id. In that column could be 20 1's, 10 2's, 5 3's, 7 4's, 100 5's, etc. Type_id is finite, it only goes from 1 to 7.
Rather than do this:
..etc, I'd really like to find a quick way to grab all of the counts and echo them out in one hit (rather than echo $n1, echo $n2, echo $n3, etc. GROUP BY is probably something that I should get my head around, but not sure how to execute that here.
Even better, would be to present that using the json_encode function, but I realise I might be pushing it
Hopefully someone can help
Following on from some other threads, I'm trying to get to grips with the new ways of PDO, PHP and MySQL and I'm mathematically failing.
Basically, I have a table with a column called type_id. In that column could be 20 1's, 10 2's, 5 3's, 7 4's, 100 5's, etc. Type_id is finite, it only goes from 1 to 7.
Rather than do this:
Code:
$nRows = $pdo->query('SELECT type_id, count(*) FROM table')->fetchColumn();
$n1 = $pdo->query('SELECT type_id, count(*) FROM table WHERE type_id=1')->fetchColumn();
$n2 = $pdo->query('SELECT type_id, count(*) FROM table WHERE type_id=2')->fetchColumn();
..etc, I'd really like to find a quick way to grab all of the counts and echo them out in one hit (rather than echo $n1, echo $n2, echo $n3, etc. GROUP BY is probably something that I should get my head around, but not sure how to execute that here.
Even better, would be to present that using the json_encode function, but I realise I might be pushing it
Hopefully someone can help