So as normal PHP query returns an array it's never been explained to me how you actually extract the value from the query out.
$countThemes = Singlequery('SELECT COUNT(1) FROM items WHERE type = :type',
array(':type' => 'theme'), $conn);
This is my query and it returns;
Array
(
[0] => Array
(
[COUNT(1)] => 5
)
)
I need the value 5 so far I've setup this below but I'm not sure what to call because I can't call a column name like a normal Select * query.
<?php foreach ($countThemes as $countTheme) : ?>
<a href="#" class="btn view-all">View All <?= $countTheme['name']; ?></a>
<?php endforeach; ?>
Use an alias for the return of the COUNT
function:
$countThemes = Singlequery('SELECT COUNT(1) AS num_items FROM items WHERE type = :type',
array(':type' => 'theme'), $conn);
Then, your array should have the index num_items
instead of COUNT(1)
.
This will also work :
echo $result[0]['COUNT(1)'];
However, using an alias in the SQL query - as suggested by nickb - is more elegant in my opinion.