kjavia795 Posted January 6, 2008 Share Posted January 6, 2008 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]"; Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/ Share on other sites More sharing options...
phpQuestioner Posted January 6, 2008 Share Posted January 6, 2008 use mysql_num_rows for $total variable. http://us3.php.net/manual/en/function.mysql-num-rows.php Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431615 Share on other sites More sharing options...
kjavia795 Posted January 6, 2008 Author Share Posted January 6, 2008 then i get this error: Fatal error: Call to undefined function mysql_num_row() Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431794 Share on other sites More sharing options...
eRott Posted January 6, 2008 Share Posted January 6, 2008 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]; Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431795 Share on other sites More sharing options...
eRott Posted January 6, 2008 Share Posted January 6, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431801 Share on other sites More sharing options...
redbullmarky Posted January 6, 2008 Share Posted January 6, 2008 then i get this error: Fatal error: Call to undefined function mysql_num_row() FYI, it's mysql_num_rows() Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431806 Share on other sites More sharing options...
kjavia795 Posted January 6, 2008 Author Share Posted January 6, 2008 i used erott's code 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 Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431826 Share on other sites More sharing options...
redbullmarky Posted January 6, 2008 Share Posted January 6, 2008 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()); Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431837 Share on other sites More sharing options...
kjavia795 Posted January 6, 2008 Author Share Posted January 6, 2008 It said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''mybb_users' WHERE Lead LIKE '%c%'' at line 1 My MYSQL version is 5.0.45-community Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431844 Share on other sites More sharing options...
kjavia795 Posted January 6, 2008 Author Share Posted January 6, 2008 I solved it, FROM 'mybb_users' is from a string FROM mybb_users is from a table so i took the '' out from erotts code and it worked!! thx Quote Link to comment https://forums.phpfreaks.com/topic/84689-quick-help/#findComment-431845 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.