Page 1 of 1

If nothing found...

Posted: Wed Mar 17, 2004 10:13 am
by HaVoC
Ok, how would I make it so, that when a query is submitted, such as

Code: Select all

<?php
mysql_query("SELECT * FROM blah WHERE id = 9");
?>
If nothing shows, or no records are found, how do I get it to do somehting about it? Like display "No records found"

Posted: Wed Mar 17, 2004 10:21 am
by JayBird
i would do it like this

Code: Select all

$query = "SELECT * FROM blah WHERE id = 9";

$result = mysql_query($query) or die(mysql_error()); 

if (mysql_num_rows($result) == 0) {
    echo "no records found";
} else {
    // Display records
}
Mark

Posted: Sat Mar 20, 2004 9:09 pm
by HaVoC
That would be good, but what I am trying to do is this.

Everytime a person enters the website their IP is recorded and the counter is added by 1. However if their IP is already there it stays the same. How would I make it add by 1 when the IP is not found in the database?

Posted: Sat Mar 20, 2004 9:21 pm
by Illusionist
counter = counter +1;

Posted: Sat Mar 20, 2004 9:25 pm
by HaVoC
I know that, but my query is

$ip = $server['remotehost'];
SELECT * FROM ips WHERE ip = {$ip}

if $ip doesn't = to anything in the database it adds one. but if there's more than 1 IP it'll add too many to the counter

Posted: Sat Mar 20, 2004 9:39 pm
by Illusionist
ok... so whats your actual question??

Posted: Sat Mar 20, 2004 9:45 pm
by HaVoC
How can I insert the Ip of ever user that enters the site into a dtabase, and for every new IP entered in the database it adds 1

Posted: Sat Mar 20, 2004 9:51 pm
by Illusionist

Code: Select all

$sql = "SELECT * FROM table WHERE ip=$ip";
$res = mysql_query($sql);
if (mysql_num_rows($res) == 0){
  $sql = "INSERT INTO table ip,count VALUES ('$ip', '1')";
  mysql_query($sql);
}elseif (mysql_num_rows($res) == 1){
  $sql = "UPDATE table SET count=count + 1 WHERE ip='$ip'";
  mysql_query($sql);
}else{
  echo "IP was found more than once....";
}
This sin;t tested but it should work... unless i might've misspelled a few things!!

Posted: Sat Mar 20, 2004 11:58 pm
by McGruff
Why do you record IP addresses in the first place?

Posted: Sat Mar 27, 2004 2:12 pm
by HaVoC
So it doesn't add 1 to the counter if the same person enters the site.

Posted: Sat Mar 27, 2004 3:01 pm
by McGruff
Unfortunately that's not reliable. There's dynamic IPs and AOL users can even have different IPs within the same session.

Posted: Sat Mar 27, 2004 3:09 pm
by Goowe
McGruff wrote:Unfortunately that's not reliable. There's dynamic IPs and AOL users can even have different IPs within the same session.
8O I didn't even know! Glad to read about this useful information :wink:

The counter script I worked on checks to see if a cookie has been set (the cookie goes for 60 days) and if it hasn't it also checks if the IP is in the database... kinda redundant and not very useful but it keeps from double entries :wink: Also, I don't keep a number in the database of how many visitors have come... I just count the rows in the table that are inserted everytime a new visitor comes

Posted: Mon Mar 29, 2004 5:24 am
by malcolmboston
it is from my understanding that counters can never be 100% reliable and you should never take there information as gospel truth

counters are useful for getting a very good estimate for how many people you have on-line (my localhost PHPBB often gives me 2 people on-line when theres only me)