Welcome Guest, Not a member yet? Register   Sign In
Retrieving the results of a selectSum query
#1

I am trying to find the sum total of a table column. The table contains details of documents including the file sizes which are in a column called 'dsize'.
Accoding to https://codeigniter.com/user_guide/datab...ilder.html, $builder->selectSum() should give me what I want.

The documentation is as follows:
PHP Code:
<?php 
PHP Code:
$builder->selectSum('age'); 
PHP Code:
$query $builder->get(); 
PHP Code:
// Produces: SELECT SUM(age) as age FROM mytable 

That is all.
Using this example:
PHP Code:
    $builder=$this->db->table('drawings');
    $builder->selectSum('dsize');
    $query=$builder->get(); 
The contents of $query - as revealed by print_r() - are as follows:
Code:
CodeIgniter\Database\MySQLi\Result Object
(
    [connID] => mysqli Object
        (
            [affected_rows] => 1
            [client_info] => mysqlnd 8.1.2-1ubuntu2.14
            [client_version] => 80102
            [connect_errno] => 0
            [connect_error] =>
            [errno] => 0
            [error] =>
            [error_list] => Array
                (
                )

            [field_count] => 1
            [host_info] => Localhost via UNIX socket
            [info] =>
            [insert_id] => 0
            [server_info] => 5.5.5-10.6.12-MariaDB-0ubuntu0.22.04.1-log
            [server_version] => 100612
            [sqlstate] => 00000
            [protocol_version] => 10
            [thread_id] => 18379
            [warning_count] => 0
        )

    [resultID] => mysqli_result Object
        (
            [current_field] => 0
            [field_count] => 1
            [lengths] =>
            [num_rows] => 1
            [type] => 0
        )

    [resultArray] => Array
        (
        )

    [resultObject] => Array
        (
        )

    [customResultObject] => Array
        (
        )

    [currentRow] => 0
    [numRows:protected] =>
    [rowData] =>
)
The 'drawings' table is as follows:
Code:
id |  name      |       date          | dsize
-----------------------------------------------------
3  | Berta Road | 2022-11-14 19:54:10 | NULL
6  | LaDePa pla | 2023-04-25 12:57:34 | NULL
7  | LYCOPODIUM | 2023-05-31 16:13:50 | NULL
8  | DRA GLOBAL | 2023-06-21 10:15:35 | NULL
9  | DRA GLOBAL | 2023-06-21 10:15:36 | NULL
10 | ADP 2440   | 2023-07-28 20:15:05 | 526528
11 | ADP 2440   | 2023-07-29 07:11:42 | 683072
12 | D G Steel  | 2023-07-29 10:08:41 | 528288
13 | DG Steel   | 2023-07-29 10:08:44 | 1054304

I really can't see how to get the total file size from the table.
~ Dave
Reply
#2

You're not running the query yet. Add another line:
PHP Code:
$sum $query->getRow(); 
Reply
#3

Thank you.

I'll try that.
~ Dave
Reply
#4

Please mark as solved if it works for you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB