SELECT COUNT giving 0 HELP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MIGO
Forum Newbie
Posts: 9
Joined: Mon Mar 02, 2009 3:13 pm

SELECT COUNT giving 0 HELP

Post by MIGO »

hey guys, I am doing a control list using ip's so I want to count how many ip's with the same number do I have in my database equal to the person that is submiting the form in my webpage.

So im using this code:

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
 
$checkin_ip = ('SELECT COUNT(ip_address) FROM `spam` WHERE `ip_address` = "$ip"'); 
 
$ipget = mysql_query($checkin_ip);
$ipid = mysql_fetch_array($ipget);
$ipid = $ipid[0];
basically I am getting the number 0, I am doing this test with my own ip and I have 11 not 0 :X

when I do print $ip; he gives me my ip but it is not working the query I do not know what is going on.

help me with the code to extract correctly the amount from the query.

cheers
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: SELECT COUNT giving 0 HELP

Post by jayshields »

What data type is the ip_address field? If it's not VARCHAR you'll need to get rid of your double quotes.
MIGO
Forum Newbie
Posts: 9
Joined: Mon Mar 02, 2009 3:13 pm

Re: SELECT COUNT giving 0 HELP

Post by MIGO »

its varchar :X
MIGO
Forum Newbie
Posts: 9
Joined: Mon Mar 02, 2009 3:13 pm

Re: SELECT COUNT giving 0 HELP

Post by MIGO »

Problem solved basically the error was in this detail in red(bold):

$checkin_ip = ("SELECT COUNT(ip_address) FROM `spam` WHERE `ip_address` = '$ip'");

I've copy the query from mysql :X and did not change that ;)
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: SELECT COUNT giving 0 HELP

Post by miro_igov »

Try this:

Code: Select all

echo '"$ip"';
and the result will be "$ip", i guess you do not save the word "$ip" in your spam table.

When you surround variable in apostrophe it does not evaluate it, if you surround in quotes it will do. Apostrophes are used for literal strings.


You can try this:

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
 
$checkin_ip = ('SELECT COUNT(ip_address) FROM `spam` WHERE `ip_address` = "'.$ip.'"');
 
$ipget = mysql_query($checkin_ip);
$ipid = mysql_fetch_array($ipget);
$ipid = $ipid[0];
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: SELECT COUNT giving 0 HELP

Post by BomBas »

You should try this one:

Code: Select all

 
$ip = $_SERVER['REMOTE_ADDR'];
 
$checkin_ip = 'SELECT COUNT(ip_address) AS addresses FROM `spam` WHERE `ip_address` = "'. $ip. '"';
 
$ipget = mysql_query( $checkin_ip );
$ipid = mysql_fetch_assoc( $ipget );
$ips = $ipid['addresses'];
 
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: SELECT COUNT giving 0 HELP

Post by jayshields »

Yeah, what they said. Sorry, I should've spotted that!
Post Reply