1

At first, I have got the following:

$sql="SELECT sum(importe_subproyecto) as importe_total FROM presupuestos_pmt WHERE oferta_numero = '$oferta_numero'";

    $result = mysqli_query($con,$sql);

    while ($row = mysqli_fetch_assoc($result))
    { 
       $importe_total = $row['importe_total'];
    }

which stores the sum of "importe_total" in a variable of the same name. Then, there is the following:

$sql="UPDATE presupuestos_pmt SET `importe_total` = '$importe_total' WHERE `oferta_numero` = '$oferta_numero'";
  mysqli_query($con,$sql);

which introduces the variable calculated previously in the rows of interest.

And finally,

$rowcount = mysqli_affected_rows($con);

Which counts the affected rows.

The script works correctly when all the fields "importe_subproyecto" are greater than 0, but row_count returns 1 when one of the fields "importe_subproyecto" is 0 even if there are 3,4,5... updated rows.

Do somebody know why?

EXAMPLE:

enter image description here

When summing the project 0002, this would give a row count of 1 because one of the fields importe_subproyecto is 0

alberzyzz
  • 277
  • 2
  • 15
  • `which stores the sum of "importe_total" in a variable of the same name.`? which variable? also update will give you only those rows which are actually updated.if you are updating same record to a row then update will not count it as `affected-rows` – Alive to die - Anant Aug 24 '17 at 10:52
  • variable: `$importe_total` – alberzyzz Aug 24 '17 at 10:54
  • Yes, I understood this but I will clarify in the original post – alberzyzz Aug 24 '17 at 10:55
  • There's no need to loop over the first query results. As your sum doesn't include a GROUP BY clause, you'll always get only one result. – alpadev Aug 24 '17 at 11:09
  • The strange thing is that I get the correct result (1,2,3...) when all `importe_subproyecto` are >0 – alberzyzz Aug 24 '17 at 11:11
  • Why do you need a summed, potentially wrong field in your table when you could just query the sum in real time for your needs ? – alpadev Aug 24 '17 at 11:27
  • It was done for testing. Anyway, I would like to know why if all the fields `importe_total` are being updated, `$row_count` returns a wrong value when one or more `importe_subproyecto` is 0 – alberzyzz Aug 24 '17 at 11:36

0 Answers0