Simple visits count with PHP/SQL

Discussion in 'PHP' started by baduist, Mar 23, 2009.

  1. #1
    Hi,

    I'm trying to build a quick script that does the following:
    1. Get user's IP address
    2. Check against SQL if it was previously there.
    3. If it's not there, store it with some extra data.
    4. Later, get the data by IP.

    My code is now:
    on header.php (appears at the top of all pages on my site):
    
    $p = $_GET['p']; // get some info rom the URL
    $theIp = $_SERVER['REMOTE_ADDR']; // Get the IP address
    
    $con = mysql_connect('host', 'user 'pass');
    mysql_select_db('mydb');
    
    $checkip = mysql_query("SELECT FROM mytable WHERE ip='$theIp'"); // check if IP is in my db...
    
    /* and ome testing t see if I got it right: */
     echo $theIp;
     if (!$checkip) { echo '1'; } else { echo '2'; };
     if ($checkip==$theIp) { echo '1'; } else { echo '2'; };
     echo $checkip; 
    PHP:
    =====> Obviously something's not right, which is why I'm here:)

    Can anyone help?
     
    baduist, Mar 23, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    set the db credentials such as host, user and pass
    then change this line $checkip = mysql_query("SELECT FROM mytable WHERE ip='$theIp'"); which should be
    $checkip = mysql_query("SELECT * FROM mytable WHERE ip='$theIp'");
     
    crivion, Mar 23, 2009 IP
  3. baduist

    baduist Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, crivion!

    I changed the query to use a "*", but now I get
    "Resource id #73" as the value of "$checkip", so I can't realyl compare it to $theIp...

    How do I fix this?


    Thanks again,
    Baduist
     
    baduist, Mar 23, 2009 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    i'm not sure, it seems the query doesn't return anything so add this red line after the query

    $checkip = mysql_query("SELECT * FROM mytable WHERE ip='$theIp'"); // check if IP is in my db...
    $checkip = mysql_num_rows($checkip);
     
    crivion, Mar 23, 2009 IP
  5. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #5
    You may need an additional , in your mysql_connect statement , is it not throwing syntax error ? where are you using $con ?
     
    it career, Mar 23, 2009 IP
  6. baduist

    baduist Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks crivion.
    mysql_num_rows($checkip) give me 75 rows.

    Unless I add "LIMIT 1" to the query.

    it career - I don't get a syntax error... what do you mean when you ask where do I use $con? (it is used in header.php if that what you mean).
     
    baduist, Mar 23, 2009 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    How about doing it this way:

    
    $p = $_GET['p']; // get some info rom the URL
    $theIp = $_SERVER['REMOTE_ADDR']; // Get the IP address
    
    $con = mysql_connect('host', 'user', 'pass');
    mysql_select_db('mydb');
    
    $getip = mysql_query("SELECT * FROM mytable WHERE ip='$theIp'");
    $numrows = mysql_num_rows($getip);  
    
    if ($numrows == 0) {
    do something if nothing was found in database }
    elseif ($numrows == 1) {
    do something if IP found in database }
    
    PHP:
     
    PoPSiCLe, Mar 23, 2009 IP