If nothing found...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

If nothing found...

Post 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"
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post 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?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

counter = counter +1;
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

ok... so whats your actual question??
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Why do you record IP addresses in the first place?
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post by HaVoC »

So it doesn't add 1 to the counter if the same person enters the site.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Unfortunately that's not reliable. There's dynamic IPs and AOL users can even have different IPs within the same session.
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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)
Post Reply