1

I need yours help with counting issues, I have array from browser SESSION

$co = array_unique($_SESSION['number']);

array will be like below

Array ( [0] => 0701 [1] => 0537 [2] => 0649 [3] => 0703 [4])

now i will use $co to perform SQL query

$result_eed = mysql_fetch_assoc(mysql_query("SELECT `number` FROM baza WHERE `Number`='$co'  AND `Number` NOT LIKE  '%ERROR%'"));

Now is my question how to return sum of all findings not separate value for each array. I need to have ONE number for all value from array, sum

1
  • You should really consider switching to MySQLi or PDO. P.S. Chaining mysql_fetch_assoc and mysql_query like that is bad practice. You should be checking if mysql_query threw any errors before trying to fetch its rows. Commented Oct 8, 2015 at 15:19

2 Answers 2

2

First off $co is an array, so you can't do "`Number` = '$co'". You'll need to use:

"`Number` IN (" . implode(',', $co) . ")"

To get the sum, just use the SUM() function:

SELECT SUM(Number) as Total
Sign up to request clarification or add additional context in comments.

1 Comment

$result_eed = mysql_fetch_array(mysql_query("SELECT SUM( DISTINCT Customer) as Total FROM report WHERE Name IN ('. implode(',', $co) .') AND Customer NOT LIKE '%ERROR%'")); echo $result_eed[Total]; and still this same, no results
1

You should use IN clause in MySQL. For that convert your array in appropriate string.

$new_String = "";
foreach($co as $c){
    $new_String .= "'"$c."',";
}
$new_String = rtrim($new_String);

$result_eed = mysql_fetch_assoc(mysql_query("SELECT SUM(`number`) FROM baza WHERE `Number`IN ($new_String)  AND `Number` NOT LIKE  '%ERROR%'"));

Check this reference:

PS: I think @Rocket's solution is better. You can also use implode (built in php function to convert array in to string).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.