mforan Posted April 30, 2008 Share Posted April 30, 2008 My question is simple really, is there anyway to make this run faster or not load as much "junk" lol? and also, out of interest, when you use code like this, should there be a limit on how many u should update? ie, because of lagg etc or slowdown? heres the code in question: <? $loadedupdateusers=true; $query = "UPDATE users SET "; $occurence = 0; for ($i = 0; $i < count($original); $i++) { if ($original[$i] != $info[$i]) { if ($occurence!=0) { $query.=","; } switch($i) { case 0: $query .= "fullname"; break; case 1: $query .= "email"; break; case 2: $query .= "cemail"; break; case 3: $query .= "username"; break; case 4: $query .= "password"; break; case 5: $query .= "squadname"; break; case 6: $query .= "lastclick"; break; case 7: $query .= "protection"; break; case 8: $query .= "rank"; break; case 9: $query .= "lastday"; break; case 10: $query .= "lasthour"; break; case 11: $query .= "money"; break; case 12: $query .= "land"; break; case 13: $query .= "people"; break; case 14: $query .= "homes"; break; case 15: $query .= "farms"; break; case 16: $query .= "factories"; break; case 17: $query .= "hangars"; break; case 18: $query .= "turrets"; break; case 19: $query .= "repairbays"; break; case 20: $query .= "satellites"; break; case 21: $query .= "naval"; break; case 22: $query .= "art"; break; case 23: $query .= "lab"; break; case 24: $query .= "eb4"; break; case 25: $query .= "eb5"; break; case 26: $query .= "eb6"; break; case 27: $query .= "eb7"; break; case 28: $query .= "eb8"; break; case 29: $query .= "eb9"; break; case 30: $query .= "eb10"; break; case 31: $query .= "barren"; break; case 32: $query .= "spies"; break; case 33: $query .= "freeland"; break; case 34: $query .= "alliance"; break; case 35: $query .= "lastminute"; break; case 36: $query .= "networth"; break; case 37: $query .= "timer"; break; case 38: $query .= "messagecount"; break; case 39: $query .= "newscount"; break; case 40: $query .= "anewscount"; break; case 41: $query .= "t80"; break; case 42: $query .= "bmp"; break; case 43: $query .= "mi28"; break; case 44: $query .= "ah64"; break; case 45: $query .= "rah66"; break; case 46: $query .= "f16"; break; case 47: $query .= "f117"; break; case 48: $query .= "f22"; break; case 49: $query .= "b52"; break; case 50: $query .= "b2"; break; case 51: $query .= "icbm"; break; case 52: $query .= "bmls"; break; case 53: $query .= "nuke"; break; case 54: $query .= "u4"; break; case 55: $query .= "u5"; break; case 56: $query .= "u6"; break; case 57: $query .= "u7"; break; case 58: $query .= "u8"; break; case 59: $query .= "u9"; break; case 60: $query .= "u10"; break; case 61: $query .= "t80a"; break; case 62: $query .= "bmpa"; break; case 63: $query .= "mi28a"; break; case 64: $query .= "ah64a"; break; case 65: $query .= "rah66a"; break; case 66: $query .= "f16a"; break; case 67: $query .= "f117a"; break; case 68: $query .= "f22a"; break; case 69: $query .= "b52a"; break; case 70: $query .= "b2a"; break; case 71: $query .= "icbma"; break; case 72: $query .= "bmlsa"; break; case 73: $query .= "nukea"; break; case 74: $query .= "ua4"; break; case 75: $query .= "ua5"; break; case 76: $query .= "ua6"; break; case 77: $query .= "ua7"; break; case 78: $query .= "ua8"; break; case 79: $query .= "ua9"; break; case 80: $query .= "ua10"; break; case 81: $query .= "aid"; break; case 82: $query .= "power"; break; case 83: $query .= "banned"; break; case 84: $query .= "img"; break; case 85: $query .= "theme"; break; case 86: $query .= "premium"; break; case 87: $query .= "txn_id"; break; case 88: $query .= "admin"; break; case 89: $query .= "hacker"; break; case 90: $query .= "ppemail"; break; case 91: $query .= "ros"; break; case 92: $query .= "breason"; break; case 93: $query .= "lastip"; break; case 94: $query .= "mteam"; break; case 95: $query .= "chatban"; break; case 96: $query .= "secret"; break; case 97: $query .= "vid"; break; case 98: $query .= "killedu"; break; case 99: $query .= "lostu"; break; case 100: $query .= "wonl"; break; case 101: $query .= "lostl"; break; case 102: $query .= "points"; } $query .= "='$info[$i]'"; $occurence=1; } } $query .= " WHERE username='$user'"; if ($occurence != 0) { mysql_query($query) or die("Error Writing to Database: ".mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/ Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 Honestly? That's probably the shortest you'll get it. >_> If you're doing what I think you're doing. Updating each field based on what's in $original and $info. >_> Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530650 Share on other sites More sharing options...
moselkady Posted April 30, 2008 Share Posted April 30, 2008 Maybe you can have all your strings in an array and this eliminate the switch block $fields = array("fullname", "email", "cemail", "username", ....); ... ... if ($occurence!=0) { $query.=","; } $query .= $fields[$i]; $query .= "='$info[$i]'"; $occurence=1; } } Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530654 Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 You could do that. Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530655 Share on other sites More sharing options...
colombian Posted April 30, 2008 Share Posted April 30, 2008 Well, you can create a function, though the variable passing would make it long, but still a bit shorter than what it is. (Not fully working function, but to give you an idea) function query($varName, $caseNum) { case $caseNum; $query .= "$varName"; break; } Basically to handle the repeating nature of that. So you'll just call: query('fullname', 0); query('email', 1); If you don't care about seeing the case number beside the query, you can also go ahead and create a loop and pass it to the original function. Like: function query($varName) { global $i; case $i; $query .= "$varName"; break; } So you'll just call it: // loop $i first (being passed as global in the function). query("fullname"); Hope this helps. Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530658 Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 I like moselkady's idea better if he wants condensed code. =P Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530660 Share on other sites More sharing options...
mforan Posted April 30, 2008 Author Share Posted April 30, 2008 hey thanks for the replys guys, i see where ur coming from etc with those ideas. but with this code, lets say there were alot more updates than there is currently to it, would u say it would slow down the server or wouldnt it really matter? Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530666 Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 You should probably use something like array_diff() and only update fields that changed from the original. Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530669 Share on other sites More sharing options...
colombian Posted April 30, 2008 Share Posted April 30, 2008 It depends on the server and the number of queries. If you are talking thousands and all updated at the same time, by many people, it could cause a slow down. If you are talking a couple of hundred, and only a few people will be performing the change, then it should not be an issue. The two main things you need to look at are: Number of concurrent users Number of changes to the database I am not an expert, but I've done some reading and testing on the subject. MySQL support like 4 billion records per table, so I wouldn't be too worried on that end. (Unless your server isn't that great, and you got tons of concurrent users doing the changes. Link to comment https://forums.phpfreaks.com/topic/103626-long-code-can-it-be-shorter/#findComment-530674 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.