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 have a php script that I'm trying to pull some data from a
I am trying to pull up some data. Here is the setup: A [school]
I am trying to extract data from this String: Hello there. Blah blahblah blah
So I'm trying to call an Oracle stored procedure from my C# .NET application.
I'm trying to build a system for programmatically filtering timeseries data and wonder if
I am a web developer who is trying to transition to Objective-C and Cocoa-touch.
I'm new to postgres, so please take it easy on me. I'm trying to
I feel like an idiot because I've spent a long time trying to find
hey there everyone. so, have a c# windows form that uses crystal reports and

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.