Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对于每个/query不';我不能给出正确的值_Php_Mysql_Sql_Postgresql - Fatal编程技术网

Php 对于每个/query不';我不能给出正确的值

Php 对于每个/query不';我不能给出正确的值,php,mysql,sql,postgresql,Php,Mysql,Sql,Postgresql,我的数据库中有一个表。我想从leave表中获取ltotal值,然后计算所有ltotal。以下是我使用的查询和代码: $annual_query = pg_query("select ltotal from leave where lapplicant='adam' and ltype=2"); $annual_result = pg_fetch_array($annual_query); if (pg_num_rows($annual_quer

我的数据库中有一个表。我想从
leave
表中获取
ltotal
值,然后计算所有
ltotal
。以下是我使用的查询和代码:

$annual_query = pg_query("select ltotal from leave where lapplicant='adam' and ltype=2");

            $annual_result = pg_fetch_array($annual_query);

            if (pg_num_rows($annual_query) > 0) {

                foreach ($annual_result as $data) {

                    $total_annual = $total_annual + $data;

                }

                print($total_annual);
            }
表中有3条记录,其中
lapplicant='adam'
ltype=2

每个
ltotal
都是1

当我尝试运行
print($total\u annual)
时,结果是2(必须是3)

然后我试着打印($annual_result['ltotal']),结果只有1(必须是1,1,1)


有人能帮我吗?谢谢。泰米尔人指出了眼前的问题。但是,为什么不在SQL中执行此操作呢

select sum(ltotal)
from leave 
where lapplicant='adam' and ltype=2

泰米尔人指出了眼前的问题。但是,为什么不在SQL中执行此操作呢

select sum(ltotal)
from leave 
where lapplicant='adam' and ltype=2
pg_fetch_array()
只返回一行,其中包含数字键和关联键(遍历时相同值两次)。您应该使用
pg\u fetch\u all()
并在连续行上遍历或使用while循环

$total_annual = 0;
$annual_query = pg_query("select ltotal from leave where lapplicant='adam' and ltype=2");
while ($row = pg_fetch_array($annual_query)) {
    $total_annual = $total_annual + $row['ltotal'];
}
print($total_annual);

pg_fetch_array()
只返回一行,其中包含数字键和关联键(遍历时相同值两次)。您应该使用
pg\u fetch\u all()
并在连续行上遍历或使用while循环

$total_annual = 0;
$annual_query = pg_query("select ltotal from leave where lapplicant='adam' and ltype=2");
while ($row = pg_fetch_array($annual_query)) {
    $total_annual = $total_annual + $row['ltotal'];
}
print($total_annual);


初始化$total_annual=0;您可能需要显示count($annual_query),以确保获得三条记录。或$recCounter=0;foreach{..$recCounter++;..}print_r($recCounter);要知道实际处理的记录数,我已初始化“$total\u annual=0;”在
$annual\u result
之后调用
$annual\u result
=6。我应该把初始化放在哪里?你能把答案写进去吗?这很难理解。。谢谢,我使用count($annual\u query),它显示1.initialize$total\u annual=0;您可能需要显示count($annual_query),以确保获得三条记录。或$recCounter=0;foreach{..$recCounter++;..}print_r($recCounter);要知道实际处理的记录数,我已初始化“$total\u annual=0;”在
$annual\u result
之后调用
$annual\u result
=6。我应该把初始化放在哪里?你能把答案写进去吗?这很难理解。。谢谢,我使用count($annual_query),它显示为1。那么,您仍然需要从result对象获取结果。这只是意味着你不需要循环来做加法。我建议学习SQL+PHP入门教程。同样,考虑使用PDO。因此,在那个查询之后,我应该使用PGyFutChyObjor?<代码> PGyFETCHORY结果($ANUALALQUIQUE,0, 0)< /代码>。请参阅手册。那么,您仍然需要从result对象中获取结果。这只是意味着你不需要循环来做加法。我建议学习SQL+PHP入门教程。同样,考虑使用PDO。因此,在那个查询之后,我应该使用PGyFutChyObjor?<代码> PGyFETCHORY结果($ANUALALQUIQUE,0, 0)< /代码>。请参阅手册。这是可行的,但如果你需要的信息只有总数,你应该用克雷格的答案。我假设您给出的代码只是一个早期测试,失败了(通常总和与上面显示的列表一起使用,这样您就不需要也不应该对它进行单独的db查询-只需在获取/遍历数据时进行计算),但如果总和是您需要的唯一信息,您应该使用Craig的答案。我假设您给出的代码只是一个失败的早期测试(通常总和与上面显示的列表一起使用,这样您就不需要也不应该对其进行单独的db查询—只需在获取/遍历数据时进行计算)