sandrob57 Posted July 3, 2007 Share Posted July 3, 2007 Anyway to simplify/reduce the amount of queries I use here (they will get loaded on every page by hundreds of users simultaneously)? It's an IP system Im building to record the IP's of my users. If working correctly, it should add a new entry each time their IP changes, and keep up to 10 records of IP changes. //This basicaly checks if you have an row AT ALL in the ip history table. If not, it creates one $result = dbquery("SELECT user_id FROM fusion_ip WHERE user_id='".$userdata['user_id']."' ORDER BY date DESC LIMIT 1"); while ($data = dbarray($result)) { if ($data['user_ip'] != $_SERVER['REMOTE_ADDR']) { //Inserts your IP data dbquery("INSERT INTO fusion_ip (user_id, ip, date) VALUES ('".$userdata['user_id']."','".$_SERVER['REMOTE_ADDR']."','".time()."')"); } } if ($result != $userdata['user_id']){ //This adds a new peice into your history $result = dbquery("INSERT INTO fusion_ip (user_id, ip, date) VALUES ('".$userdata['user_id']."','".$_SERVER['REMOTE_ADDR']."','".time()."')"); //This deletes your ip history if you have over 10 stored in DB $result = dbquery("SELECT ip_id FROM fusion_ip WHERE user_id='".$userdata['user_id']."'"); $total = 0; $delete_amount = 0; while ($data = dbarray($result)) { $total =+1; } if ($total > 10) { $delete_amount = $total - 10; $result = dbquery("SELECT user_id FROM fusion_ip WHERE user_id='".$userdata['user_id']."' ORDER BY date ASC LIMIT ".$delete_amount.""); while ($data = dbarray($result)) { $result = dbquery("DELETE FROM fusion_ip WHERE ip_id='".$data['ip_id']."'"); } } } Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 3, 2007 Share Posted July 3, 2007 ??? whats the your concern Quote Link to comment Share on other sites More sharing options...
sandrob57 Posted July 3, 2007 Author Share Posted July 3, 2007 ??? whats the your concern I want to reduce the amount of queries if possible, or make this more efficient. I'm just asking if there is a better way of writing this. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 3, 2007 Share Posted July 3, 2007 heres my version i havent test it but its better i guess $result = dbquery("SELECT user_id FROM fusion_ip WHERE user_id='".$userdata['user_id']."' ORDER BY date DESC LIMIT 1"); while ($data = dbarray($result)) { if ($data['user_ip'] != $_SERVER['REMOTE_ADDR']) { //Inserts your IP data dbquery("INSERT INTO fusion_ip (user_id, ip, date) VALUES ('".$userdata['user_id']."','".$_SERVER['REMOTE_ADDR']."','".time()."')"); } } if ($result != $userdata['user_id']){ //This adds a new peice into your history $result = dbquery("INSERT INTO fusion_ip (user_id, ip, date) VALUES ('".$userdata['user_id']."','".$_SERVER['REMOTE_ADDR']."','".time()."')"); //This deletes your ip history if you have over 10 stored in DB $result = dbquery("SELECT ip_id FROM fusion_ip WHERE user_id='".$userdata['user_id']."'"); $delete_amount = 0; $total =sizeof(dbarray($result)); if ($total > 10) { $delete_amount = $total - 10; $result = dbquery("SELECT user_id FROM fusion_ip WHERE user_id='".$userdata['user_id']."' ORDER BY date ASC LIMIT ".$delete_amount.""); $result = dbquery("DELETE FROM fusion_ip WHERE ip_id IN (".implode(',',dbarray($result)).")"); } } Quote Link to comment 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.