PHP + SQL Add two values from the same column

0

I have a table like this:

Ineedtoaddthevalueofid1tothevalueofid15,savinginavariableinPHP.

I'veresearchedalot,butIdidnotgetsomethingspecificforthat.Thanksforanyhelp!

NOTE:Ihavetryingthefollowingbutitisreturning1,whentherightoneistoreturn49,14:

$result=mysqli_query($conn,"SELECT SUM(valor) FROM tb_valores WHERE id = ($id1) OR id = ($id15) ");

echo $linhasemuso = mysqli_affected_rows($conn);
    
asked by anonymous 26.07.2018 / 22:55

3 answers

0

If someone is experiencing the same problem, here's the answer:

$result=mysqli_query($conn,"SELECT SUM(valor) AS conta FROM tb_valores WHERE id = 1 OR id = 15 ");
$row = mysqli_fetch_assoc($result); 
echo $sum = $row['conta'];
    
26.07.2018 / 23:45
0

That's the answer that works, but it's a very big code I'd like to reduce. Is someone enabled?

    
26.07.2018 / 23:32
0

The logic is simple, but it works, if you adapt, you can do a generator of querys and insert new Ids whenever necessary:     

$execute = mysqli_query($conexao, "SELECT * FROM tabela_assim WHERE id = '$Id1' OR Id = '$Id2'");

$soma_total = 0;

while($line = mysqli_fetch_array($execute)){

$soma = $line['valor'];

$soma_total = $soma + $soma_total;
}

?>
    
26.07.2018 / 23:12