GRR! Simple problem with mysql - HELP!!

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
Right i have this code to count the total unique hits on my site. What i am doing is checking weather the IP adress of the user and if that IP is the same as it is what is in the database then it wont log it, otherwise it logged the ip.

Here is what i got so far
Code:
$ip = $_SERVER['REMOTE_ADDR'];
   $uniquetotal = mysql_query("SELECT * FROM uniquetotal") or die(mysql_error());
   
   if($ip = mysql_fetch_array($uniquetotal)){
   }
   else{
    $ip = $_SERVER['REMOTE_ADDR'];
    mysql_query("INSERT INTO uniquetotal(ip) VALUES('$ip')") or die(mysql_error());
   }

Its just blocking all of the IP adress though! AHH, I did it before but i just cannot remember how to though!

Good rep if someone helps me please!!
 
Last edited:

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
In this bit of the code:
PHP:
if($ip = mysql_fetch_array($uniquetotal)){
   }
There should be two '=' signs (It should be '==')

Also you haven't told it what to do if the IP does equal one in the database.
Maybe it would be better to do this:
PHP:
$ip = $_SERVER['REMOTE_ADDR'];
   $uniquetotal = mysql_query("SELECT * FROM uniquetotal") or die(mysql_error());
   
   if($ip !== mysql_fetch_array($uniquetotal)) {
    $ip = $_SERVER['REMOTE_ADDR'];
    mysql_query("INSERT INTO uniquetotal(ip) VALUES('$ip')") or die(mysql_error());
   }
 
Last edited:

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
i tried that - it just skips to the else even if it is the same or not.

and when it does equal to the database nothing is supposed to happen because it only adds when it doesnt exsist
 
Last edited:

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
Have you tried this:
PHP:
$ip = $_SERVER['REMOTE_ADDR'];
   $uniquetotal = mysql_query("SELECT * FROM uniquetotal") or die(mysql_error());
   
   if($ip !== mysql_fetch_array($uniquetotal)) {
    $ip = $_SERVER['REMOTE_ADDR'];
    mysql_query("INSERT INTO uniquetotal(ip) VALUES('$ip')") or die(mysql_error());
   }
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
PHP:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
   $uniquetotal = mysql_query("SELECT ip FROM uniquetotal where ip = $ip") or die(mysql_error());
   $db_query = mysql_fetch_array($db_query, MYSQL_ASSOC);

   if(mysql_num_rows($db_query)==0) {
    $ip = $_SERVER['REMOTE_ADDR'];
    mysql_query("INSERT INTO uniquetotal(ip) VALUES('$ip')") or die(mysql_error());
   } 

?>

You can used this way better because only one record fetch, no load on your db.
 

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
i would probs not use phpasks one because i only like to use codes that i understand - if i knew well then i would go for it, but it dazzles me :). I would have used it but techash's was the one i was looking for ;)
Edit:
OK - dont worry, what i had there was perfectly fine, it was just that i needed the "WHERE" in it
Code:
			$ip = $_SERVER['REMOTE_ADDR'];
			$uniquetotal = mysql_query("SELECT * FROM uniquetotal WHERE ip = '$ip'") or die(mysql_error());
			
			if($ip = mysql_fetch_array($uniquetotal)){
			}
			else{
				$ip = $_SERVER['REMOTE_ADDR'];
				mysql_query("INSERT INTO uniquetotal(ip) VALUES('$ip')") or die(mysql_error());
			}
 
Last edited:

phpasks

New Member
Messages
145
Reaction score
0
Points
0
i would probs not use phpasks one because i only like to use codes that i understand - if i knew well then i would go for it, but it dazzles me :). I would have used it but techash's was the one i was looking for ;)
Edit:
OK - dont worry, what i had there was perfectly fine, it was just that i needed the "WHERE" in it
Code:
            $ip = $_SERVER['REMOTE_ADDR'];
            $uniquetotal = mysql_query("SELECT * FROM uniquetotal WHERE ip = '$ip'") or die(mysql_error());
            
            if($ip = mysql_fetch_array($uniquetotal)){
            }
            else{
                $ip = $_SERVER['REMOTE_ADDR'];
                mysql_query("INSERT INTO uniquetotal(ip) VALUES('$ip')") or die(mysql_error());
            }

This is not right way.
This is wrong.

You can used my code, Your problem solved.
PHP:
 <?php
$ip = $_SERVER['REMOTE_ADDR'];
   $uniquetotal = mysql_query("SELECT ip FROM uniquetotal where ip = $ip") or die(mysql_error());
   $db_query = mysql_fetch_array($db_query, MYSQL_ASSOC);

   if(mysql_num_rows($db_query)==0) {
    $ip = $_SERVER['REMOTE_ADDR'];
    mysql_query("INSERT INTO uniquetotal(ip) VALUES('$ip')") or die(mysql_error());
   } 

?>
 
Top