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?
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'");
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
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);
You may need an additional , in your mysql_connect statement , is it not throwing syntax error ? where are you using $con ?
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).
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: