How to list database time count result

Asked

Viewed 59 times

2

I’m wanting to list a table record and display the sum of reported hours, but I’m not getting it.

The Reports table has the Registration and Duration field

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

I want to list the sum of hours(Duration) for each record(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>
}
  • Return an error? What is the output? What is your select returning?

  • Warning: mysql_fetch_row() expects Parameter 1 to be Resource, string Given in /home/fabvirtual/public_html/portal/comgap-hours.php on line 127

  • What is line 127?

  • while($Row=mysql_fetch_row($hours)){

  • And executing the command directly on the bank, the SELECT, which is the output?

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 the field Registration I see that will return the sum of the duration of each Registration which I believe is what you want. It will group the values (from the field Registration) which are equal and will make the sum of the duration for each Registration. Understands?

Difference between mysql_fetch_assoc and mysql_fetch_row:

mysql_fetch_row - Get a line as a numeric array

mysql_fetch_assoc - Get a result row as an associative matrix

The method mysql_result:

Returns data from the result.

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

Remarks:

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

References and other information:

http://php.net/manual/en/function.mysql-fetch-assoc.php

http://php.net/manual/en/function.mysql-fetch-row.php

http://php.net/manual/en/function.mysql-result.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.