简体   繁体   中英

Best way to update a large amount of items in a MySQL Database

I'd like to think that there's a simple way of doing this. I have over thirty columns in a database that I need to update. All of the columns have the same name as the correlating session variables.

Here's my code to fetch the array and assign variables:

    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'];
    }

Basically I'd like to update each row with a (possibly) changed session variable on an update page. Is there a way to iterate through the session variables and update the corresponding columns? Note: I have other session variables set that are not in the DB.

What about:

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

fetch them all to associative arrays

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM