在循环期间使用一会儿mysql_fetch_array和UPDATE [英] use a while mysql_fetch_array and UPDATE during the loop

查看:146
本文介绍了在循环期间使用一会儿mysql_fetch_array和UPDATE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个表中提取数据,将列与变量进行比较,然后如果它们匹配,则将它们添加并更新另一张表上的字段.似乎UPDATE在while循环内仅工作一次,并将该时间的值放在整个表的该列的每一行中.奇怪的是,当我回显它时,所有值都是正确的(唯一的)..但是只有当我回显...当我查看带有终端的表时,该列的所有行都是相同的.我读到您一次不能使用2个查询...然后再次...我读到您可以的,如果第一个查询不在循环中...请帮助...

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...

我想在每一行中添加field1和field2并将总计放入总计行中.

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

这是代码....

<?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 />";

}

?>

在这里,回声给了我

苏珊14

3月18日

鲍勃13

山姆21

这是数据库

mysql> SELECT total_val FROM table;

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

ech总数正是我想要的.我只需要将它们放在total_val列中即可.

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

推荐答案

更新while循环

当前,当while循环到达迭代末尾时,它具有这样的功能,它的总数为21,并且更新表的值为21,因此所有行都包含21.

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.

您的更新查询每次都会更新所有行 在此处输入要更新的唯一ID的条件

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']");

这篇关于在循环期间使用一会儿mysql_fetch_array和UPDATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆