Forum Moderators: coopster

Message Too Old, No Replies

Very simple adding array

         

bobnew32

3:47 am on Aug 9, 2003 (gmt 0)

10+ Year Member



Ok the query is "Select downloads from screen_saver"

Downloads is a variable in every row in that database. I want to add all the variables of downloads up, but I am really unsure how to do this. I think its a for each loop, but those are really tricky for me. Thx for anyone who decides to help me.

bcolflesh

4:14 am on Aug 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT SUM(downloads) FROM screen_saver

bobnew32

4:24 am on Aug 9, 2003 (gmt 0)

10+ Year Member



Then would the whole thing be

$sql= "SELECT SUM(downloads) FROM screen_saver";
$result= mysql_query($sql);
print $result;

? Thanks cuz its just returning resource id 6 or somthing like that.

bcolflesh

4:42 am on Aug 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to connect to a server first - read this:

php.net/manual/en/function.mysql-connect.php

related:

php.net/manual/en/function.mysql-query.php

bobnew32

5:02 am on Aug 9, 2003 (gmt 0)

10+ Year Member



Lol your funny....:P. Yes I know how to connect to a server and I am already connected to one.

jatar_k

7:54 am on Aug 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no, its because all you are printing is a resource id.

$sql= "SELECT SUM(downloads) FROM screen_saver";
$result= mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
print_r $row;
}

bobnew32

5:05 pm on Aug 9, 2003 (gmt 0)

10+ Year Member



Jatar, me putting in your code gives me an error on the print_r $row; line. I tried different variations of it, do you have an error in your code?

martekbiz

8:14 pm on Aug 9, 2003 (gmt 0)

10+ Year Member



try:

$sql= "SELECT SUM(downloads) AS totaldownloaded FROM screen_saver";
$result= mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
print_r $row;
}

note the "AS totaldownloaded " part after the SUM()

Aaron

bobnew32

1:43 am on Aug 10, 2003 (gmt 0)

10+ Year Member



Aaron, what does that piece of code do/mean? And I keep getting the error on the piece of code thats

print_r $row;

Please help!

martekbiz

1:46 pm on Aug 10, 2003 (gmt 0)

10+ Year Member



Hey, it should be print_r($row);

or better yet, echo "<pre>".print($row)."</pre>";

that'll show you all the variables contained within the query that you can use.

Aaron

jatar_k

4:31 pm on Aug 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I wondered about that actually.

I did forget the parentheses, it is just a recursive print, you dont need to do it that way, it was more for examples sake.

$sql= "SELECT SUM(downloads) AS totaldownloaded FROM screen_saver";
$result= mysql_query($sql);
$row = mysql_fetch_array($result)
echo $row['totaldownloaded'];

should work a little better.

bobnew32

5:30 pm on Aug 10, 2003 (gmt 0)

10+ Year Member



Thankyou for everyone that helped me solve/create this coding, I really appreciate it. Escpecially Jatar for tolerating my never ending php/mysql questions :P