访问结果集行中的值,该值来自 MySQL 函数调用 [英] Access value in result set row where the value comes from a MySQL function call

查看:39
本文介绍了访问结果集行中的值,该值来自 MySQL 函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,作为正常的 PHP 查询返回一个数组,从来没有向我解释过当列值是 MySQL 函数调用的返回值时,您实际上是如何从查询的结果集数组中提取值的.

So as normal PHP query returns an array it's never been explained to me how you actually extract the value from the query's result set array when the column value is the return value of a MySQL function call.

$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
        )
)

我需要值 5.到目前为止,我已经在下面进行了设置,但我不确定该调用什么,因为我无法像普通的 Select * 查询那样调用列名.

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; ?>

推荐答案

使用别名返回 COUNT 函数:

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);

然后,您的数组应该具有索引 num_items 而不是 COUNT(1).

Then, your array should have the index num_items instead of COUNT(1).

这篇关于访问结果集行中的值,该值来自 MySQL 函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆