Jayram121 Posted August 12, 2013 Share Posted August 12, 2013 I am trying to update my table using while loop, unfortunately it is only updating the first record. Why can I not update the whole 27000 records? Thank you. <?php $sql = "SELECT * FROM spammers ORDER BY spamID;"; $res = mysql_query($sql); if(!$res) { trigger_error("Could not connect to the database!\n <br/>MySQL Error: " . mysqli_connect_error()); } while ($rows=mysql_fetch_assoc($res)) { $sql = "UPDATE spammers SET abc='". 5252 ."' WHERE spamID ='".$rows['spamID']."';"; $res = mysql_query($sql); if(!$res) { die(" Could not query the database: <br/>". mysql_error() ); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/281066-updating-1000%E2%80%99s-records/ Share on other sites More sharing options...
sasa Posted August 12, 2013 Share Posted August 12, 2013 He change name of variable $res in this part of code while ($rows=mysql_fetch_assoc($res)) { $sql = "UPDATE spammers SET abc='". 5252 ."' WHERE spamID ='".$rows['spamID']."';"; $res = mysql_query($sql); // change to something diferent if(!$res) { // and in this line die(" Could not query the database: <br/>". mysql_error() ); } Quote Link to comment https://forums.phpfreaks.com/topic/281066-updating-1000%E2%80%99s-records/#findComment-1444496 Share on other sites More sharing options...
requinix Posted August 12, 2013 Share Posted August 12, 2013 You're overwriting $res inside the loop. You know you can just update everything in one statement? UPDATE spammers SET abc=5252If you've hidden something important and can't do that (like you don't actually want to update all of them) then I guarantee you there's a much better way of doing it than the horribly inefficient SELECT/UPDATE loop you have now. Quote Link to comment https://forums.phpfreaks.com/topic/281066-updating-1000%E2%80%99s-records/#findComment-1444498 Share on other sites More sharing options...
Jayram121 Posted August 12, 2013 Author Share Posted August 12, 2013 This the code. $sql = "SELECT * FROM spammers ORDER BY spamID;"; $res = mysql_query($sql); if(!$res) { trigger_error("Could not connect to the database!\n <br/>MySQL Error: " . mysqli_connect_error()); } while ($rows=mysql_fetch_assoc($res)) { if(!filter_var( $rows['email'], FILTER_VALIDATE_EMAIL)) { if(filter_var( $rows['countery'], FILTER_VALIDATE_EMAIL)) { $sql = "UPDATE spammers SET email='".$rows['countery'] ."' WHERE spamID ='".$rows['spamID']."';"; $res = mysql_query($sql); if(!$res) { die(" Could not query the database: <br/>". mysql_error() ); } } } } requinix can you advice me what is the better way of doing it than the horribly inefficient SELECT/UPDATE loop . Thank u Quote Link to comment https://forums.phpfreaks.com/topic/281066-updating-1000%E2%80%99s-records/#findComment-1444501 Share on other sites More sharing options...
requinix Posted August 12, 2013 Share Posted August 12, 2013 If you're alright with a simpler email validity check, maybe like "*@*.*", then UPDATE spammers SET email = countery WHERE email NOT REGEXP ".+@.+\..+" AND countery REGEXP ".+@.+\..+" Quote Link to comment https://forums.phpfreaks.com/topic/281066-updating-1000%E2%80%99s-records/#findComment-1444554 Share on other sites More sharing options...
Jayram121 Posted August 13, 2013 Author Share Posted August 13, 2013 Thank you requinix. also can you please tell me what is the better way of doing SELECT/UPDATE loop . Quote Link to comment https://forums.phpfreaks.com/topic/281066-updating-1000%E2%80%99s-records/#findComment-1444701 Share on other sites More sharing options...
requinix Posted August 13, 2013 Share Posted August 13, 2013 I just did. Quote Link to comment https://forums.phpfreaks.com/topic/281066-updating-1000%E2%80%99s-records/#findComment-1444712 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.