This is what my table (TimeTable) looks like...
name time
allen 400
horn 300
What I am trying to do is to pull allen and 300 out of the db and horn and 300. Before I display them so the screen I need to do some math with the time. How do I pull out individuals rows then work with one of the attributes? The display will look like...
Allen 3hrs 2 minutes (arbitrary numbers here)
Horn 2hrs 12 minutes (arbitrary numbers here)
Thanks in advance.
Simple SQL query help
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
SELECT name, time FROM tablenameyou could use a while loop to cycle through all the rows. something like this:
All this does is it goes through the table one row at a time and u can do whatever you want in the while loop
Code: Select all
<?php
$query = "SELECT * FROM Table";
$result = mysql_query($query) or die("Couldn't Execute Query ".mysql_error());
while ($row = mysql_fetch_array($result))
{
//Do whatever calculations you want. Here $row is an array
//where all of the indexes are column names
//for example: $row['name'] would output allen the first loop, then horn the second time
}
?>- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
If you are using MySQL you could use one of these queries:
query 1) In case 'time' is in seconds:
which returns:
query 2) In case 'time' is in minutes:
which returns:
Regards,
Scorphus.
query 1) In case 'time' is in seconds:
Code: Select all
select name, sec_to_time(time) as time_fmt from tablename;Code: Select all
+-------+----------+
| name | time_fmt |
+-------+----------+
| allen | 00:05:00 |
| horn | 00:06:40 |
+-------+----------+
3 rows in set (0.09 sec)Code: Select all
select name, sec_to_time(time*60) as time_fmt from tablename;Code: Select all
+-------+----------+
| name | time_fmt |
+-------+----------+
| allen | 05:00:00 |
| horn | 06:40:00 |
| horn | 06:40:00 |
+-------+----------+
3 rows in set (0.00 sec)Scorphus.