Jump to content

Recommended Posts

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

 

 

 

 

A friend gave me this code but I get an error mysql_fetch_array(): supplied argument is not a valid MySQL result resource:

 

$total=mysql_query("sum(totalearnings) FROM 'mybb_users' WHERE Lead LIKE '%c%' ");

$total=mysql_fetch_array($total);

$total="$total[0]";

Link to comment
https://forums.phpfreaks.com/topic/84689-quick-help/
Share on other sites

Maybe I am missing something, but how is it you are able to use the same variable name for three different values?

 

$total=mysql_query("sum(totalearnings) FROM 'mybb_users' WHERE Lead LIKE '%c%' ");
$total=mysql_fetch_array($total);
$total="$total[0]";

 

First you are saying the value of $total is the SQL query, then your saying the value of $total is results of that query in an array, and lastly you are defining the variable's content/value as the first value in the array.

 

I'm not sure how much sense I may or may not have just made, but perhaps your problems are coming from the fact that you use the same variable three times for three different things? Why don't you try something like:

 

$query = mysql_query("sum(totalearnings) FROM 'mybb_users' WHERE Lead LIKE '%c%' ");
$result = mysql_fetch_array($query);
$total = $result[0];

Link to comment
https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431795
Share on other sites

UPDATE:

 

After a bit of searching, I think your SQL statement may be wrong. I have never used sum myself, however, feel free to take a look HERE. I think that may be what you are trying to achieve.

 

<?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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431801
Share on other sites

here's the problem:

 

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

 

instead, you need:

 

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

Link to comment
https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431837
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.