更新MySQL数据库中大量项目的最佳方法

时间:2011-08-10 19:32:07

标签: php

我想有一种简单的方法可以做到这一点。我需要更新数据库中有超过30列。所有列都与关联会话变量具有相同的名称。

这是我获取数组和分配变量的代码:

    while ($row = mysql_fetch_array($result))
    {   
        $_SESSION['PendingCivil'] = $row['PendingCivil'];
        $_SESSION['PendingAsbestos'] = $row['PendingAsbestos'];
        $_SESSION['PendingDomestic'] = $row['PendingDomestic'];
        $_SESSION['AsgNewCivil'] = $row['AsgNewCivil'];
        $_SESSION['AsgNewAsbestos'] = $row['AsgNewAsbestos'];
        $_SESSION['AsgNewDomestic'] = $row['AsgNewDomestic'];
        $_SESSION['AsgTransferCivil'] = $row['AsgTransferCivil'];
        $_SESSION['AsgTransferAsbestos'] = $row['AsgTransferAsbestos'];
        $_SESSION['AsgTransferDomestic'] = $row['AsgTransferDomestic'];
        $_SESSION['AsgReopenedCivil'] = $row['AsgReopenedCivil'];
        $_SESSION['AsgReopenedAsbestos'] = $row['AsgReopenedAsbestos'];
        $_SESSION['AsgReopenedDomestic'] = $row['AsgReopenedDomestic'];
        $_SESSION['DispWOPCivil'] = $row['DispWOPCivil'];
        $_SESSION['DispWOPAsbestos'] = $row['DispWOPAsbestos'];
        $_SESSION['DispWOPDomestic'] = $row['DispWOPDomestic'];
        $_SESSION['DispFinalCivil'] = $row['DispFinalCivil'];
        $_SESSION['DispFinalAsbestos'] = $row['DispFinalAsbestos'];
        $_SESSION['DispFinalDomestic'] = $row['DispFinalDomestic'];
        $_SESSION['DispBTCivil'] = $row['DispBTCivil'];
        $_SESSION['DispBTAsbestos'] = $row['DispBTAsbestos'];
        $_SESSION['DispBTDomestic'] = $row['DispBTDomestic'];
        $_SESSION['DispJTCivil'] = $row['DispJTCivil'];
        $_SESSION['DispJTAsbestos'] = $row['DispJTAsbestos'];
        $_SESSION['DispJTDomestic'] = $row['DispJTDomestic'];
        $_SESSION['DispTOCivil'] = $row['DispTOCivil'];
        $_SESSION['DispTOAsbestos'] = $row['DispTOAsbestos'];
        $_SESSION['DispTODomestic'] = $row['DispTODomestic'];
        $_SESSION['OldCivil'] = $row['OldCivil'];
        $_SESSION['OldAsbestos'] = $row['OldAsbestos'];
        $_SESSION['OldDomestic'] = $row['OldDomestic'];
    }

基本上我想在更新页面上用(可能)更改的会话变量更新每一行。有没有办法迭代会话变量并更新相应的列?注意:我设置的其他会话变量不在DB中。

2 个答案:

答案 0 :(得分:1)

怎么样:

while ($row = mysql_fetch_assoc($result))
    foreach($row as $k => $v)
        $_SESSION[$k] = $v;

答案 1 :(得分:0)

将它们全部提取到关联数组

while ($row = mysql_fetch_assoc($result)){   
    foreach($row as $column_name => $data){
         $_SESSION[$column_name] = $data;
    }
}