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? Quote 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)']; Quote 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. Quote 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. Quote 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. Quote 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]'")); Quote 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. Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.