Jump to content

Simplify/Reduce these mysql queries


sandrob57

Recommended Posts

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

}

Link to comment
https://forums.phpfreaks.com/topic/58176-simplifyreduce-these-mysql-queries/
Share on other sites

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)).")");

}

 

} ::)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.