Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:27:06+00:00 2026-05-20T09:27:06+00:00

i am trying to pull data from one table, compare columns to a variable,

  • 0

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…

I want to add field1 and field2 of every row and put the total in the total row.

heres the code….

<?php

require("connection.php");


mysql_select_db("database", $connection);
echo "<br />";

$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 "<br />";

}

?>

Heres what the echo gives me

Susan 14

Mary 18

Bob 13

Sam 21

heres the database

mysql> SELECT total_val FROM table;

+-----------+
| total_val |
+-----------+
| 21 |
| 21 |
| 21 |
| 21 |
+-----------+

the ech totals are exactly what I want. I just need them put inside the total_val column.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:27:07+00:00Added an answer on May 20, 2026 at 9:27 am

    Update the specific (current) row in the while loop

    Currently it is doing like this when while loop reach at the end of iteration it has the total 21 and it updates table with value 21 hence all the rows contains 21.

    Your update Query is updating all rows every time
    put here condition for the unique id to update

    $sql=mysql_query("UPDATE table SET total_val = '$values' WHERE id=$row['id']");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to pull out data from the table I had from the
I'm listing the data from one of my models in a table <% @events.each
I'm trying to pull data using a SqlDataReader, one column of which is in
I am trying to modify a stored procedure I wrote to pull data from
I am trying to pull data from our Oracle datamart into R using RODBC.
I'm trying to use SharpZipLib to pull specified files from a zip archive. All
I would like to pull artifacts from teamcity. I've been trying to use c#
I need to pull data from two tables: Neptune_FN_Analysis and Neptune_prem There will be
I am trying to pull together some data for a report and need to
I have an SSIS package that exports data from a table on a SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.