steviemac Posted February 3, 2012 Share Posted February 3, 2012 I have about 350 rows that I have to update a single column in each row. I coded so I can do each row individually but I was wondering if there is a way to update all of them at once. Hence saving me time. This is my code: <?PHP include "dbmembers.php"; ?> <?php if(isset($submit)){ $ID = mysql_real_escape_string(trim($_POST['ID'])); $result = mysql_query("SELECT response FROM myuser WHERE ID ='$ID'"); $row = mysql_fetch_array($result); $res = hash("sha256", $row['response'] .$salt ); { echo $row['response'] ."----".$res; echo "<br /><br />"; } } mysql_query("UPDATE myuser SET response='$res' WHERE ID = '$ID'"); printf ("Updated records: %d\n", mysql_affected_rows()); ?> Thanks in advance for any advise. Quote Link to comment https://forums.phpfreaks.com/topic/256310-is-there-an-easier-to-do-update/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 3, 2012 Share Posted February 3, 2012 If you have mysql 5.5.5 (where sha2 was added to mysql), you could do everything in a single UPDATE query. For your existing code, since you need to use php to preform the sha256 hash, you would need to select each value, modify it using php code, and then update the value. You are aware that you could automate this by forming a select query that gets all the rows and then iterate over all the rows to modify and update the values? There's no need to have any sort of form that submits one id at a time. Quote Link to comment https://forums.phpfreaks.com/topic/256310-is-there-an-easier-to-do-update/#findComment-1313936 Share on other sites More sharing options...
steviemac Posted February 3, 2012 Author Share Posted February 3, 2012 I am not aware of this or how it is even done Quote Link to comment https://forums.phpfreaks.com/topic/256310-is-there-an-easier-to-do-update/#findComment-1313942 Share on other sites More sharing options...
PFMaBiSmAd Posted February 3, 2012 Share Posted February 3, 2012 Just about every good php/mysql book or tutorial shows how to loop over a result set to process all the rows that a query matches. See the 'Fetch Array While Loop' example at this link - http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php Quote Link to comment https://forums.phpfreaks.com/topic/256310-is-there-an-easier-to-do-update/#findComment-1314064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.