Reputation: 41
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
Upvotes: 1
Views: 45
Reputation: 227240
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
Upvotes: 2
Reputation: 5792
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).
Upvotes: 1