Mundo Posted March 6, 2009 Share Posted March 6, 2009 if($_GET['module'] == "add") { include "tpl/addtodiary.tpl"; if(isset($_GET['submit'])) { $result = mysql_query(sprintf("SELECT diary_duration FROM diary WHERE diary_date = '$_GET[date]'")); while($row = mysql_fetch_array($result)) { echo $row['diary_duration']; } } } This creates and array and outputs it to screen, how can I add all these results together? I'm making a booking system so I need to see if there is time available on selected day... Is there a add() function or something similar? Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/ Share on other sites More sharing options...
Maq Posted March 6, 2009 Share Posted March 6, 2009 If I understand you correctly use: $result = mysql_query(sprintf("SELECT SUM(diary_duration) FROM diary WHERE diary_date = '$_GET[date]'")); $row = mysql_fetch_array($result) echo $row['SUM(diary_duration)']; Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/#findComment-778429 Share on other sites More sharing options...
alphanumetrix Posted March 6, 2009 Share Posted March 6, 2009 Are you trying to create a loop? If so, try the foreach function. Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/#findComment-778433 Share on other sites More sharing options...
Mundo Posted March 6, 2009 Author Share Posted March 6, 2009 If I understand you correctly use: $result = mysql_query(sprintf("SELECT SUM(diary_duration) FROM diary WHERE diary_date = '$_GET[date]'")); $row = mysql_fetch_array($result) echo $row['SUM(diary_duration)']; So you think: if($_GET['module'] == "add") { include "tpl/addtodiary.tpl"; if(isset($_GET['submit'])) { $result = mysql_query(sprintf("SELECT diary_duration FROM diary WHERE diary_date = '$_GET[date]'")); $row = mysql_fetch_array($result); echo $row['SUM(diary_duration)']; } } Because that just returns a blank. Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/#findComment-778436 Share on other sites More sharing options...
Mundo Posted March 6, 2009 Author Share Posted March 6, 2009 Well i'm not necessarilly trying to use a loop... I just thought I would have to. I'm trying to get the SUM of everything in the diary_duration column. Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/#findComment-778437 Share on other sites More sharing options...
Maq Posted March 6, 2009 Share Posted March 6, 2009 You didn't change this line...: $result = mysql_query(sprintf("SELECT SUM(diary_duration) FROM diary WHERE diary_date = '$_GET[date]'")); Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/#findComment-778439 Share on other sites More sharing options...
Mundo Posted March 6, 2009 Author Share Posted March 6, 2009 Ah, I didn't realise that that had been changed. Thanks again, you've really helped me today. Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/#findComment-778445 Share on other sites More sharing options...
Maq Posted March 6, 2009 Share Posted March 6, 2009 Ah, I didn't realise that that had been changed. Thanks again, you've really helped me today. No problem. Are you trying to create a loop? If so, try the foreach function. He already had a while loop to display each diary_duration but he wanted the sum of all of them, and MySQL has a simple built in SUM function. Link to comment https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/#findComment-778447 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.