Forum Moderators: coopster

Message Too Old, No Replies

php, mysql, phpbb script

         

Diceman

9:19 pm on Jul 25, 2006 (gmt 0)

10+ Year Member



i am trying to graph some phpBB information in cacti, i have most of my scripts working except one. i need a script that will select all the values from a column and then add all the values together. one script that is working currently is this:

<?
// reset guests, registered and total
$total = 0;

// db connection
$hostname = "";
$username = "";
$password = "";
$dbname = "";

// connect to database
$connection = mysql_connect($hostname,$username,$password) or die ("Cannot connect to server.");
$db = mysql_select_db($dbname,$connection) or die ("Could not select database.");

// count total cars in database
$sql = "SELECT COUNT(id) AS cars FROM phpbb_garage WHERE id>0";
$total = mysql_result(mysql_query($sql, $connection), 0,"cars");

// close the db-connection
mysql_close($connection);

// generate the output
$output="total:" . $total . "";

// print the output
echo "$output";
?>

if i use this script for the item i cant get working, it returns the incorrect value. i figured out that it is totaling all the rows that meet the criteria, not totalling the values of the rows that meet the criteria. any help is appreciated. i barely squeezed by with the script above. ;-)

im trying to count the total of new private messages a user has. some users have more than 1 so a simple COUNT(new_private_messages) WHERE new_private_messages>0 wont work. thanks for any help.

FalseDawn

11:54 pm on Jul 25, 2006 (gmt 0)

10+ Year Member



You need to use SUM, not COUNT

Diceman

2:56 am on Jul 26, 2006 (gmt 0)

10+ Year Member



i am such a disgrace. thanks for your help. that was totally easier than i thought it would be. didnt help that our local sysadmin at work didnt think of it first when i asked for his guidance in the general direction.

coopster

10:55 am on Jul 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Diceman.


our local sysadmin at work didnt think of it first when i asked for his guidance in the general direction

Many sysadmins aren't adept at writing SQL, so you can't hold that against the chap. Note, I said many, not all -- so any future readers that are both do not take offense to this message and know that my hat is off to you ;)

Diceman

4:53 pm on Jul 26, 2006 (gmt 0)

10+ Year Member



well, the reason i asked him in the first place is because he is rather a "jack of all trades" and does a little bit of everything. he was thinking that i could query all the rows and then put them into an array and then have one more line that adds all the array variables together. the SUM function is much nicer. i even thought that there might be one but i feel that the mysql user manual is so freaking long and disorganized, at least for my thinking, that i cant ever find what im looking for in there.

coopster

10:35 pm on Jul 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Search for "sql tutorials" and the like instead. You'll find that it is SQL you want to learn, not just MySQL. MySQL is but one of the relational database management systems at your disposal.