- PHP - 结果集数组的访问值
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 (  => 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
$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
This will also work :
However, using an alias in the SQL query - as suggested by nickb - is more elegant in my opinion.
echo $ result  ['COUNT （1）'];
</ code> </ pre>
但是，在我看来，使用SQL查询中的别名 - 正如nickb所建议的那样 - 更优雅。</ p> \ n </ div>