• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

little help

kjavia795

New Member
I have a table called mybb_users with a column called totalearnings and another column called Lead

OK I want a php code that will only look at all people (the rows) who have a Lead column that has the letter c in it, and add up every single number in the totalearnings column and give me the total. I only need the total, not a whole list of all the people... and make sure that it only adds up the ppl who have the letter "c" included in the Lead column... thx :)


EDIT:
so far i got:

<?php
$query = mysql_query("SELECT SUM(totalearnings) FROM 'mybb_users' WHERE Lead LIKE '%c%'");
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$total = $row['SUM(totalearnings)'];
echo "Total Earnings: $" . $total;
}
?>

and it said query was empty.... but it isnt... when i go into the mysql, i select the LIKE %...% option and put c into it, and it displays all of the numbers... so i need it to add it up somehow... i think its the Lead LIKE thing that isnt working
 
Last edited:
you should use the SUM() function in the query. Also do a check for rows.

Code:
if (msql_num_rows($query >0) {
// While statement here
} else {
echo "Error... no rows in the database.";
}

Also, you're querying a query. If you're doing it the way I see you want to do it... it'd be like this:

Code:
<?php
$query = "SELECT SUM(totalearnings) FROM 'mybb_users' WHERE Lead LIKE '%c%'";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)){
$total = $row['totalearnings'];
echo "Total Earnings: $" . $total;
}
} else {
echo "Error... no rows in the database.";
}
?>
 
Back
Top