How to list result of the database count

2

I want to list a table record and display the sum of reported times, but I can not.

The reports table has the field registration and duration

registration       duration
    2501       00:40:00(timestamp)  
    2501       01:20:00(timestamp)  
    5531       01:20:00(timestamp) 

I want to list the duration of each registration

$hours = mysql_query("SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( duration ) ) )  AS duration_sum FROM reports");                                                 
$horas = mysql_result($hours,0,"duration_sum");
while($row=mysql_fetch_row($horas)){

    <tr>
    <td align="center"><div align="left"><? echo($row[1]); ?></div></a></td>
    <td align="center"><div align="center"><? echo($row[2]); ?></div></td>
    </tr>
}
    
asked by anonymous 09.11.2015 / 23:25

1 answer

0

Example:

$hours = mysql_query("SELECT registration, duration FROM reports GROUP BY registration");

while($row = mysql_fetch_assoc($hours)){ 
    <tr>
        <td align="center"><div align="left"><? echo $row['registration']; ?></div></a></td>
        <td align="center"><div align="center"><? echo $row['duration']; ?></div></td>
    </tr>
}

Grouping by registration field I see that it will return the sum of the duration of each registration that I believe is what you want. It will group the values (from the registration field) that are the same and will add the duration for each registration . Do you understand?

Difference between mysql_fetch_assoc and mysql_fetch_row :

mysql_fetch_row - Get a row as a numeric array

mysql_fetch_assoc - Get a result row as an associative array

The mysql_result method:

Returns result data.

  mysql_result () returns the contents of a cell of the MySQL result. The field argument can be the field index, the field name, the table point, the field name (table.field). If the column name uses a nickname ('select foo as bar from ...'), use the nickname instead of the column name.

Notes :

Consider replacing mysql_ * with mysqli_ *, because the one you are using is obsolete.

References and other information :

link

link

link

    
09.11.2015 / 23:59