Jump to content

[SOLVED] mysql_fetch_array and counting


Mundo

Recommended Posts

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.