Note that there are some explanatory texts on larger screens.

plurals
  1. POuse a while mysql_fetch_array and UPDATE during the loop
    text
    copied!<p>i am trying to pull data from one table, compare columns to a variable, then if they match, add them and update a field on another table. It seems that the UPDATE only works once inside the while loop, and puts the value of that one time in every row of that column throughout the table. Odd thing is, when I echo it, all the values are correct (unique)..but only when I echo...when I look at table with terminal, all the rows of that column are identical. I have read that you cannot use 2 query at a time...then again...I have read that you can, if the first one is outside the loop...please help...</p> <p>I want to add field1 and field2 of every row and put the total in the total row.</p> <p>heres the code....</p> <pre><code>&lt;?php require("connection.php"); mysql_select_db("database", $connection); echo "&lt;br /&gt;"; $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { $values = ($row['field1'] + $row['field2']); echo "values = " . $values; $sql=mysql_query("UPDATE table SET total_val = '$values'"); echo $row['user_name'] . " " . $values; echo "&lt;br /&gt;"; } ?&gt; </code></pre> <p>Heres what the echo gives me</p> <p>Susan 14</p> <p>Mary 18</p> <p>Bob 13</p> <p>Sam 21</p> <p>heres the database</p> <pre><code>mysql&gt; SELECT total_val FROM table; +-----------+ | total_val | +-----------+ | 21 | | 21 | | 21 | | 21 | +-----------+ </code></pre> <p>the ech totals are exactly what I want. I just need them put inside the total_val column.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload