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

}

 

} ::)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.