Jump to content

Finding unique IP addresses


sotusotusotu

Recommended Posts

Hey,

 

I have a column in my database called adverts_user_ip and has lots of records of ip addresses in there. I would like to be able to show unique ip addresses, so in order to do this I am guessing I need to somehow go through them  all and determine if there are any repeats.

 

Does anyone have any ideas how to do this or am I going about this is the wrong way?

 

thanks

Link to comment
Share on other sites

Thanks for the reply. I am not sure if I explained myself properly. I need to find out the amount in numbers, rather than the ip addresses themselves.

 

For example my adverts table looks like this:

 

adverts_id   

adverts_img   

adverts_company   

adverts_url   

adverts_user_ip   

adverts_day   

adverts_month   

adverts_year

 

In the adverts_user_ip row there a lots of records of addresses, but a there are lots of duplicates. I want to be able to distinguish between clicks and unique clicks?

 

Link to comment
Share on other sites

Hey,

 

Still no luck, not returning any errors just not returning any results either. Here is the code I am using:

 

$query = "SELECT DISTINCT count(adverts_user_ip) FROM adverts";
								$result = mysql_query($query);

								while($row = mysql_fetch_array($result))
								{

									echo "UNIQUE IP :{$row['adverts_user_ip']} <br />";
								}

Link to comment
Share on other sites

$sql = "SELECT adverts_user_ip FROM adverts GROUP BY advers_user_ip;";

$sqlresult = mysql_query($sql);

$num_ip_addr = mysql_num_rows($sqlresult);

That should get what you need.  Each row of the query is going to have a unique ip address.  Count the number of rows and you'll have the number of unique ip addresses.

Link to comment
Share on other sites

Thanks mate, I realized I was being dumb by not displaying $result variable and trying to display the row. Stupid me, it has been a long day. I edited my code and got a weird result: "Unique IP :Resource id #2"

 

 

$query = "SELECT DISTINCT count(adverts_user_ip) FROM adverts";
								$result = mysql_query($query);

								while($row = mysql_fetch_array($result))
								{

									echo "Unique IP :$result <br>";
								}

 

I also tried your code and it returned nothing.

 

When i put your sql statement into mine it returned returned:

 

"Unique IP :Resource id #2

Unique IP :Resource id #2

Unique IP :Resource id #2"

 

 

Link to comment
Share on other sites

When i put your sql statement into mine it returned returned:

 

"Unique IP :Resource id #2

Unique IP :Resource id #2

Unique IP :Resource id #2"

 

You're getting that because you're trying to output the resource returned by mysql_query().  You should be using the array returned by mysql_fetch_assoc() or mysql_fetch_array().

 

Here's some code that should work using my query.

 

$query = "SELECT adverts_user_ip FROM adverts GROUP BY advers_user_ip;";

$result = mysql_query($query);
$num_ip_addr = mysql_num_rows($sqlresult);

while($row = mysql_fetch_assoc($result))
{
  echo "Unique IP : $row[adverts_user_ip] <br>";
}

echo "Total number of IPs: $num_ip_addr<br>";

 

 

Link to comment
Share on other sites

With Jesirose code it at least returns "16"

$query = "SELECT DISTINCT count(adverts_user_ip) AS numIPs FROM adverts";
								$result = mysql_query($query);

								$row = mysql_fetch_array($result);

								//$num_ip_addr = mysql_num_rows($result);

								echo $row['numIPs'];


								mysql_query( $sql, $dbConn );

								mysql_close($dbConn);

 

I still need to find the unique IP's though, not the total

Link to comment
Share on other sites

I haven't been able to get my unique ones to return a result yet. This is all my code:

mysql_select_db( "andy", $dbConn) or die("unable to select database");

								$query = "SELECT DISTINCT count(adverts_user_ip) AS numIPs FROM adverts";
								$result = mysql_query($query);

								$row = mysql_fetch_array($result);

								//$num_ip_addr = mysql_num_rows($result);

								echo $row['numIPs'];


								mysql_query( $sql, $dbConn );

								mysql_close($dbConn);

Link to comment
Share on other sites

Try this.

 

mysql_select_db( "andy", $dbConn) or die("unable to select database");

$query = "SELECT adverts_user_ip FROM adverts GROUP BY advers_user_ip;";

$result = mysql_query($query);
$num_ip_addr = mysql_num_rows($sqlresult);

while($row = mysql_fetch_assoc($result))
{
  echo "Unique IP : $row[adverts_user_ip] <br>";
}

echo "Total number of IPs: $num_ip_addr<br>";

mysql_close($dbConn);

 

BTW, why do you have so many tabs in your code?

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.